Travis Risner

Ranch Hand
+ Follow
since Jun 29, 2007
Travis likes ...
Python VI Editor Postgres Database
Cows and Likes
Cows
Total received
1
In last 30 days
0
Total given
0
Likes
Total received
7
Received in last 30 days
0
Total given
1
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Travis Risner

Hi Mike,

First, since I have not worked with Heroku, I do not know what it needs to work or not work.  Congratulations for getting the application to work in that environment.

1. Virtual environment, I understand what it is, I get that we create it to not break anything in our "language files".



In Python, the general practice for non-trivial projects is to create the virtual environment first, then start building the application.  That way any libraries needed for the application are installed in the virtual environment and won't interfere with any other project.  A source control tool -- such as git -- is often invoked at the beginning as well.

2. I want to push my app also to git hub.



Github accepts whatever is pushed from the local git repository.  (If there are exceptions, I am not aware of them.)  Using git in the local repository is a great tool to help going from messy to nice.  When the project is ready for production (generally in the master branch), push it up to Github.

Both the "before Heroku" folder and the "with Heroku" folder can be pushed to Github.  If there are files and folders that should not go to Github, add a file called ".gitignore" and add those items to it.  (Details and format of the .gitignore file are on Github.)

Although doing this is fairly straightforward, many of the higher-end Python IDE's can make all this relatively painless.

HTH,
Travis
1 year ago
I'm guessing that the program is attempting to run on a version of Python older than Python 3.6, which the code I shared requires.  The most recent release available is 3.8.  If an upgrade is needed, may I suggest upgrading directly to 3.8?

Also, the most recent release of PyCharm CE is 2019.3.  The recent versions of PyCharm understand f-strings and the many other recent additions to Python that make life easier.  The newer versions of PyCharm can assist with writing code.
1 year ago
Actually there is a library called kivy https://pypi.org/project/Kivy/ that "With the same codebase, you can target Windows, macOS, Linux, Android and iOS".  

Back when computers were slow, developers used languages such as C to get all the performance possible out of those pokey machines.  Now, computers (even Rasberry Pi's) are so powerful we optimize for development time instead.  That's where Python shines.  Backed by ease of coding, over 200,000 libraries of utilities and a rapidly growing community of users, Python is becoming very popular.  Yes, some of the most powerful libraries such as pandas, numpy and scipy are written in C, which give you great performance with an easy-to-use interface.  Also, Python isn't just a "toy" language.  Companies such as YouTube, Dropbox, Pinterest, and Instagram have all staked their businesses on Python.  

Hopefully you will find that Python lets you get more done (machine learning, web development, GUI applications, etc.) in less time.
1 year ago
I happened to run it on a Mac, which is a variation on BSD Linux.  However, the psutil library is platform agnostic so it shouldn't matter.  Did you have problems running it?

As to the value for i in Print_Numbers, I changed it to use the number passed in when the instance of Counting_SysInfo was instantiated at the very bottom of the module.  Perhaps the integer literal of 100_000 was confusing?  One of the thoughtful changes added in Python 3.6 was allowing an underscore in numeric literals as a "thousands" separator.  Python has a number of enhancements - such as generators, list comprehensions, tuples, etc. - not found in other major languages that make life easier for the software developer.
1 year ago
Hi Zulfi,

Essentially I was suggesting that the "process.xxx" be replaced by "psutil.xxx" for the three function calls, and that psutil.cpu_percent be called twice -- once before calling the code to be measured and again after.

You requested the complete program.  Here is what I eventually ended up with.  Some comments about alterations that went beyond just adjusting the original code.
*   Added more comments so that when I come back to it the comments and code will quickly remind me of how to make use of this library.
*   The code has been rearranged to emphasize what needs to be done in what order.
*   Code that measures the process from the code that measures system-wide information has been separated.
*   Added code to show the use of both kinds of cpu_percent.
*   Used the bytes2human function to format the various values rather than guessing at their magnitude.
*   Added some returns.  They are completely optional, but they make a great place to set a breakpoint when debugging.
*   Apparently, psutil multiplies all "percentage" results by 100.  This program backs that off so they can be displayed properly.
*   Formatted the percentages so that useless precision is eliminated.
*   The underscore variable is a Python convention for capturing and throwing away a some value that we don't care about.
*   Sometimes when I ran this program, the overall cpu percentage was still zero.  Other times I got a reasonable value.



Sample output:



HTH,
Travis
1 year ago
Hi Zulfi,

The psutil class has both class methods and instance methods.  There is so much information for each method, the distinction gets lost.  Some of the functions in the program need to be called directly from psutil.  Specifically: psutil.cpu_percent(), psutil.disk_usage and psutil.virtual_memory.  (Apparently cpu_percent is both a class method and an instance method  - but see comment next.

According to the documentation the cpu_percent() method (both kinds), must be called twice - unless an interval is specified.  The first time it is called, it "initializes" internal variables and always returns 0.0.  So what I did was to also call psutil.cpu_percent() before calling self.Print_Numbers().  In order to get the cpu percentage above zero, I also had to bump the number of iterations through Print_Numbers() up to 10,000.

BTW, in order sort this out, I ran the following code in IDLE, the interactive python console.

The *help()* function can be used for any class, import or library.

Travis
1 year ago
Hi Zllfi,

Is there a limit to the size of an integer?  The short answer is no.

The python documentation at https://docs.python.org/3/reference/lexical_analysis.html#operators states:

There is no limit for the length of integer literals apart from what can be stored in available memory.


Thus python integers are limited only by the amount of memory on the computer.

HTH,
Travis
1 year ago
Hi Thillai,

Unlike Java, Python uses dynamic typing which means that a variable at run time can hold, for example, a string one moment and an integer the next.  Starting with Python 3.5, a more formal way of annotating functions and methods has been defined (or recognized).  The details are given in https://www.python.org/dev/peps/pep-0484/.  As PEP 484 points out, this allows IDE's to identify and flag code that violates the annotation.

The Python annotations are very flexible.  For example, recently I wrote some code that allows a parameter to be a string, integer or an object.  Why?  All three were simply different ways to point to the same object.  This allowed the caller to pass in whatever was handy to identify the desired object.

So, although most library functions returns a specific type which is documented, there are times that a function may be deliberately ambiguous for a reason.  However, no one is suggesting that the documentation is perfect so please share which function is puzzling you.  Perhaps it can be explained better here -- and a request can be submitted to have the documentation improved.  

HTH,
Travis
1 year ago
Thanks Tim for the clarification of which Python is available on the Raspberry Pi.  Would you provide a link to the instructions you used for installing Python 3?  Rumor has it that CentOS is also converting to Python 3 as well.

it would be great if Simon can run gnuradio directly from Java like he originally tried.  I didn't look deeply enough into gnuradio to find alternate ways of interacting with it.

Yes the Python 2 End-Of-Life announcement by the Python Software Foundation was confusing.  This blog post about it was updated January 8.  The following quote was added.

Clarification Update January 8, 2020
Effective January 1, 2020, no new bug reports, fixes, or changes will be made to Python 2.

After Python 2.7.17 (October 19, 2019) was released, some additional changes accumulated before the end of 2019 when the core development team froze the 2.7 branch. As a final service to the community, python-dev will bundle those fixes -- and only those fixes -- into a final 2.7.18 release. The release date for 2.7.18 will be in April 2020 because that allows time for the release managers to complete a release candidate and final release.



1 year ago
Hi Simon,

Python has a concept of a "virtual environment" for pulling together all the requirements needed for running a nontrivial program.  This is roughly similar to a java jar file being used to hold all the libraries needed for a nontrivial Java program.  Explaining how to set up a Python virtual environment is beyond what can be explained here so please search for "Python virtual environment", "virtualenv", and "Python activate" for that information.

Once the virtual environment is set up and tested, Java will probably need to use a shell script to invoke it.  The script will probably have components that will change directories to the base of the Python project, run the virtual environment's activate command, then run your program with the "python <program.py>" command.  The shell script will also make it easier to pass parameters.

BTW, Python 2 was EOL'ed as of the end of last year.  For reasons known only to Apple, Catalina still uses Python 2.  Thus /usr/bin/python is still version 2.7.  Using Homebrew to install a modern version of python is definitely a good way to get it.  It can install a modern version of (QT5) as well as the Python bindings (pyqt), although since I have not used QT, there may be more needed.

Since this project is targeted to go on a Rasberry PI, it might be a good idea to use Homebrew to install a copy of micropython to use for this project.  That may reveal any code that may have trouble running in the Pi environment.

Apparently Java can be installed on a PI.  However, Python is native on the Pi.  Perhaps it would be worthwhile to take another look at recasting all of the Java code to Python.  It may be much easier than it first looks even preparing the GUI.

There are Python IDE's that will make all this a bit easier, such as Pycharm or Wing.  Both have "community" or "personal" editions that are free.

This may have become a bigger project than you suspected at the beginning, but I hope you will continue this to completion.    
Travis
1 year ago
Hi Dodi,

Did you try searching on "python pandas matplotlib" yet?  Are you using modern python (version 3) or jython/legacypython?  The link for pandas documentation for modern python starts at https://pandas.pydata.org/pandas-docs/stable/.  I took a closer look at matplotlib and discovered that it is only good for 2D graphs.  This link https://stackoverflow.com/questions/36589521/how-to-surface-plot-3d-plot-from-dataframe shows a hint about how to create a 3D graph.

If you are forced to use Jython/legacy Python. we will have to look further for a solution or find a way to graft a modern python solution back into your current framework.

HTH,
Travis
1 year ago