• Matteo

    @suzylw Hi, if you want to start studying openCV (that is, as I see, a fairly widespread and well-known software, so you will find several examples and explanations) without first studying CoreML that needs some objc_util to run, you can use SageMathCell as a free remote python environment to test some not-pure python libraries that can't be installed locally in Pythonista (to use the remote python engine your device must be online and you have about 2 minutes of continuous cpu calculation, due to some necessary restrictions introduced by the authors). See here to learn how to use Pythonista with the SageMath remote server.

    Try to open in your internet browser a new cell, put inside the blank window the following code, as example (or use this freeware app, very useful to perform tests):

    !pip install --quiet --disable-pip-version-check "--target=opencv-python-headless" opencv-python-headless
    sys.path.append(os.getcwd() + "/opencv-python-headless")
    
    import cv2 as cv
    import numpy as np
    import matplotlib.pyplot as plt
    # Feature set containing (x,y) values of 25 known/training data
    trainData = np.random.randint(0,100,(25,2)).astype(np.float32)
    # Labels each one either Red or Blue with numbers 0 and 1
    responses = np.random.randint(0,2,(25,1)).astype(np.float32)
    # Take Red families and plot them
    red = trainData[responses.ravel()==0]
    plt.scatter(red[:,0],red[:,1],80,'r','^')
    # Take Blue families and plot them
    blue = trainData[responses.ravel()==1]
    plt.scatter(blue[:,0],blue[:,1],80,'b','s')
    plt.show()
    
    newcomer = np.random.randint(0,100,(1,2)).astype(np.float32)
    plt.scatter(newcomer[:,0],newcomer[:,1],80,'g','o')
    knn = cv.ml.KNearest_create()
    knn.train(trainData, cv.ml.ROW_SAMPLE, responses)
    ret, results, neighbours ,dist = knn.findNearest(newcomer, 3)
    print( "result:  {}\n".format(results) )
    print( "neighbours:  {}\n".format(neighbours) )
    print( "distance:  {}\n".format(dist) )
    plt.show()
    

    press Evaluate and wait. You should see the output as described here.

    The remote server installs in a temporary folder the opencv-python library (that can't be installed locally in Pythonista, as you know).

    This is possible thanks to the work made by opencv-python author, who created a full working version of openCV for python, and by authors of the SageMathCell service.

    I wish you good experiments and good learning.
    Bye

    posted in Pythonista read more
  • Matteo

    @mikael Hi, yes you are right :-) , but also who has Pythonista 2 has no problems in any case, maybe only security problems, since Python 2.7.12 (inside Pythonista 2, that will be not update anymore) probably doesn't have all the security problems already solved in Python 2.7.16.
    I'm agree with you that the best thing is to have Pythonista with only one core (with Python 3, because it is the future...) and with site-packages folder with only libraries for it and not for Python 2 (in order to avoid taking up space with repeated libraries for both) but also with a Python 2 core available only as an option in Pythonista settings.

    Regards
    Bye

    posted in Pythonista read more
  • Matteo

    @JonB Hi, thank you for your reply, and thanks to everyone for the comments about this interesting matter (Python 2 or not Python 2...).

    I personally feel lucky because in case there will be no Python 2 in Pythonista 3 in future, on my iphone I still have Pythonista 2 which I purchased a few years ago, so I could virtually use my scripts written in Python 2 forever.

    I am sorry for those who do not have Pythonista 2, and I am sad that Apple does not allow user to install more versions of the same application on idevices (the only way is to use TestFlight but for a limited time); if Apple would allow this, there would be not problems because any user could decide to keep the current version of Pythonista 3 (which includes Python 2 and Python 3) for running scripts written in Python 2 and install also an updated version of Pythonista with only Python 3 and new and interesting features.

    Thanks
    Bye

    posted in Pythonista read more
  • Matteo

    @JonB Hi, thank you for info, so synthesizing Python 3 was born for Unicode support and Python 2 will die because Python 3 exists, in order to avoid redundancy.

    Unfortunately I'm not very expert in programming but it is a world that fascinates me so I ask you what does it mean (in link you posted ".../why-python-3-exists/amp/") that a file containing 'abcd' in Python 3 is a string and in Python 2 can be both a text file (like for Python 3) and a binary file (with bytes) that represents 97, 98, 99, 100.

    Thanks
    Bye

    posted in Pythonista read more
  • Matteo

    @ccc Hi, interesting thank you for sharing it, but I keep wondering why it was necessary to create Python 3 instead of implementing all these new features (like "f-strings", "Pathlib", "Type hinting" and others listed in the site you shared) in Python 2 with a progressive numbering in its versions (for example: in Python 2.7.15 implemented "f-strings", in Python 2.7.16 implemented "Pathlib" and "Type hinting", and so on...).

    I suppose that all the new features in Python 3 could be implemented also in Python 2 if Python 2 had the same low level features of Python 3. I mean: if "f-strings", to work properly, needs some new low level features implemented only in Python 3 core, why not implement this low level feature also in Python 2 in order to have a Python 2 capable to use "f-strings" and also the "old" format?

    In this way no need to completely change a full programming language and no problems when people want to run with Python some python 2 or 3 scripts or entire libraries. Python users could have had a single Python distribution, updated progressively, with all the new features that we find only in Python 3.

    With the hypothetical single Python distribution, user could execute directly, for example, a script where somewhere in the source there are some pieces of code that use "f-strings" and somewhere else other pieces of code that use the standard format version, without problems (the interpreter of the single Python distribution would be able to interpret/understand, in the same source script/library, the code with "f-strings" and the code with format without syntax errors).

    Thank you for sharing
    Bye

    posted in Pythonista read more
  • Matteo

    @ccc Hi, it doesn't worry me too much because I think I will continue to use my personal Python 2 scientific scripts (for work and hobby) that usually don't require any Python 3 features, but I have often asked myself some questions that I have never been able to answer.

    Really I never understood why it was useful to develop a different version of Python (why create a new big version of Python, from 2 to 3, instead to give Python 2 the same useful features of Python 3? maybe too complicated the upgrade of the existing Python 2 and more easy the develompment from zero of a new programming language that is Python 3?).

    Has Python 3 started to be developed due to several requests by users about usage of Python 2 programming language? I mean, what had prompted developers to start creating Python 3 instead of upgrading Python 2 with the additions (new/different features) requested by users that now are implemented in Python 3?

    Was it not enough to modify Python 2 with the purpose of interpreting the new Python 3 syntax, in order to have Python 2 updated with the most interesting things provided for Python 3?

    And apart from some syntax differences between Python 2 and 3 (for example the different syntax of the "print" function), what features are really more efficient in Python 3 to justify the abandonment of Python 2, and the consequent abandonment of innumerable Python 2 libraries which are no longer updated to Python 3 by the developers?

    Bye

    posted in Pythonista read more
  • Matteo

    @Squidgyd, I made some tests with Pymunk lib and SageMathCell and if user doesn't want to create games (it needs PyGame that in SageMathCell server can't work) but only perform simulations for research or study, Pymunk and SageMathCell can work pretty well togheter.

    If you want, try to download inside your Pythonista this zipped folder, extract it somewhere, open and execute script "_execute_sage.py", wait and, if you are online and your Pythonista is allowed to be online, you should see some output data and a simple t-x,y graph of a very simple simulation where a circle falls down, due to gravity, for 5 seconds from initial position x,y equal to 0,0.

    In this way, by studing and following video tutorials and some documentation about Pymunk, you could create by yourself any kind of simulation scripts with your Pythonista (unfortunately no way to use PyGame as said before), that for the Pymunk calculation asks for some helps to his friend Sage ;-)

    Happy simulations!
    Bye

    posted in Pythonista read more
  • Matteo

    @Squidgyd Hi, to test the library Pymunk (with some existing examples or with some yours test scripts) you could try to install it in Sagemathcell, but it doesn't work if the script requires PyGame (error: "No available video device"), I don't know how to install properly it in a remote Linux server like Sagemathcell, and it is a limit for a library that uses a lot of graphics.

    For example try to open this permalink that refers to a saved cell: it tries to install the required libraries but the code can not work properly due to the absence of a working method to view a video (created with PyGame) remotely.

    The only thing I can suggest is to try to ask Pymunk author if it is possible for him (if he is interested) to modify his library in order to automatically by-pass the importing of PyGame, when you run your test script, if it is not installed on the computer and instead of PyGame the modified version of Pymunk library could create a choosen number of static graphical images of the simulation (by using a different method that does not require PyGame lib) that can be transformed into a video (with some python libs) that can be viewed or downloaded from Sagemathcell. If he wants and can do this kind of modification in his lib and you want/can create a python script that automatically execute the test script and create a video of the 2D rigid body physical simulation from a set of images (in which you see the position and the real speed of the objects), it would be interesting.

    Regards
    Bye

    posted in Pythonista read more
  • Matteo

    @lpl Hi, interesting, you are right, the author (PyYoshi) used a txt file in Japanese language (about 330 kbytes) to perform the benchmark. I tried with other kind of files with same size and the ratio sometimes was about 1:500 (obviously not always 1:3600). I think the ratio could be less than 1:3600 (the average estimated by the author) with some kinds of files (maybe critical/bad for function chardet.detect?).

    Also I tried his bench script with some modifications like:

    1. variable do_times set to 10 instead 100 (to speed up the calculation)
    2. same txt file as input for function c(c)hardet.detect but I executed a benchmark for 5 files, with size x1, x2, x4, x8 and x32 (I created them simply by creating a copy of the original file with 330kbyte size and by adding the same content inside the file 2 times, 4 times, and so on..., in order to have a little set of input data with a geometric progression as size, as an example).

    I noticed that, by performing the test with my computer, with some available ram, cpu, etc...,:

    • with chardet : the time cost is quite near to follow a linear law , with an estimated time complexity of about O(k*n) where k approximately equal to 5e-4

    • with cchardet : same as chardet but with k approximately equal to 9e-8

    This thing surprises me enough, I didn't think there was such a big difference between pure-python and c for the function chardet.detect, that is c is a few thousand times faster than pure python (my test using the author's bench script is however a very simplified test and not good enough to estimate the time complexity with high precision and as much as possible independently from the computer in use).

    But I still believe that in real life with Pythonista the user can easily write scripts that he/she can test anywhere (just have the idevice in own hand) and then can export to a computer the script created with Pythonista for large input files. Pythonista in this case helps a lot in preparing the calculation script (I mean, you can verify it with little input data by using Pythonista and pure-python libraries installed via pip) and then you can use all the power of a computer when working with large input data.

    Concluding, for me the only limit with Pythonista and pure python libraries is not the performance (in most cases the user should use Pythonista like a platform to write good code, without worrying at first to execute the script with large inputs), but the impossibility to install not pure python libraries that haven't pure-python alternatives on the web.

    Bye

    posted in Pythonista read more
  • Matteo

    @lpl Hi, sorry but I don't know lxml library, never used it. But I'm agree with you that a c compiled library is faster than a pure python equivalent code. However I'm not completely agree with you about "terrible" performance. In my opinion I would consider with "terrible" performance a pure python code that runs for some hours instead of some seconds of an equivalent c compiled code. Which kind of test have you performed to state that the performance is terrible?

    In other words, with practical example: let's suppose that an algorithm has time complexity O(n^3) and you write a pure python code and the equivalent C code that both execute the algorithm. Well, based on the input (that, simplifying, can be 10kbyte, 100 kbyte, 1000 kbyte and so on), how much time the execution of the pure python code costs compared to the C one if both are executed on same machine and in the same conditions (machine with same X% free RAM and CPU power)?

    Thank you
    Bye

    posted in Pythonista read more

Internal error.

Oops! Looks like something went wrong!