Python Profiler garbage [duplicate] - python

This question already has answers here:
cProfile saving data to file causes jumbles of characters
(3 answers)
Closed 6 years ago.
I am currently profiling some python code like so:
import cProfile
cProfile.runctx('self._Foo ()', globals(), locals(), 'c:/restats.txt')
def Foo(self):
stuff
The code runs successfuly, outputs no exception. However my restats.txt looks like this:
SomeFunc( i i gÇZ3a/­?gÇZ3a/­?0( s; C:\SomeFolder\bar.pyiL t
Basically it is 150 lines of garbage characters with random paths and function names mixed in.
What could be the issue? Am I using this correctly? The python file is being loaded in through Maya if that makes a differance.

Please read the documentation for what to do after you have collected the trace information. I believe you will need to use pstats.Stats to extract the information you need.

The file is not meant to be human-readable. As described in the documentation, you can use the pstats module to load the file and explore the profiling data.

Related

How do I read from the terminal in Python? [duplicate]

This question already has answers here:
read subprocess stdout line by line
(10 answers)
Closed 16 days ago.
I just want to use os.system("dir") and also be able to save the text outputted to a variable. I tried using sys.stdout.read() but running sys.stdout.readable() returns False. Do you know how I can read from the terminal?
using os library:
info = os.popen('dir').read()
You can use the subprocess.check_output method
Example
import subprocess as sp
stdout = sp.check_output("dir")
print(stdout)
There is a bit of confusion here about the different streams, and possibly a better way to do things.
For the specific case of dir, you can replace the functionality you want with the os.listdir function, or better yet os.scandir.
For the more general case, you will not be able to read an arbitrary stdout stream. If you want to do that, you'll have to set up a subprocess whose I/O streams you control. This is not much more complicated than using os.system. you can use subprocess.run, for example:
content = subprocess.run("dir", stdout=subprocess.PIPE, check=True).stdout
The object returned by run has a stdout attribute that contains everything you need.
If you want to read just go with
x = input()
This reads a one line from the terminal. x is a string by default, but you can cast it, say to int, like so
x = int(x)

problem with with-as statement as mentioned in learn python3 the hard way [duplicate]

This question already has answers here:
What is the python "with" statement designed for?
(11 answers)
Closed 4 years ago.
I've been reading the learn python3 the hard way book and in a exercise about python symbols he refers to a 'as' symbol and in the description it says "Part of the with-as statement" and the example format is "with X as Y: pass" but i couldn't find anything about such a thing online so I'm asking here.
Does anyone know anything about it?
and as a refrence it's exercise 37
The With x as y construct in python in called a context manager.
Context managers are used to properly manage resources. For example, if one is used to open a file, a context manager will ensure the file is closed.
with open('my_file.txt', 'r') as file:
for line in file:
print('{}'.format(line))
This is equivalent to:
file = open('my_file.txt') as file
for line in file:
print('{}.format(line))
file.close()
As you can see, the call to the close function is not necessary when you use a context manager.Its easy to forget to close the file, and this can lead to your system crashing if too many files are open. (There is a maximum number allowed by the operating system.)
See this link for more information and examples.

Python - print() - debugging -show file and line number [duplicate]

This question already has answers here:
filename and line number of Python script
(11 answers)
Closed 5 years ago.
When using print() in python, is it possible to print where it was called? So the output will look like var_dump in php with xdebug. Eg. I have script D:\Something\script.py, and at line 50, there is a print("sometest"), so the output will look like this:
D:\Somethinq\script.py:50 sometest
Or is there any module that could achieve this? In large projects, it's really hard to manage where these prints came from.
So, using answers provided in filename and line number of python script , this function can be called instead of print(), and prints line number before every output:
from inspect import currentframe
def debug_print(arg):
frameinfo = currentframe()
print(frameinfo.f_back.f_lineno,":",arg)

STDIN file read query [duplicate]

This question already has answers here:
How do I read from stdin?
(25 answers)
Closed 6 years ago.
I am doing small project in which I have to read the file from STDIN.
I am not sure what it means, what I asked the professor he told me,
there is not need to open the file and close like we generally do.
sFile = open ( "file.txt",'r')
I dont have to pass the file as a argument.
I am kind of confused what he wants.
The stdin takes input from different sources - depending on what input it gets.
Given a very simple bit of code for illustration (let's call it: script.py):
import sys
text = sys.stdin.read()
print text
You can either pipe your script with the input-file like so:
$ more file.txt | script.py
In this case, the output of the first part of the pipeline - which is the content of the file - is assigned to our variable(in this case text, which gets printed out eventually).
When left empty (i.e. without any additional input) like so:
$ python script.py
It let's you write the input similar to the input function and assigns the written input to the defined variable(Note that this input-"window" is open until you explicitly close it, which is usually done with Ctrl+D).
import sys, then sys.stdin will be the 'file' you want which you can use like any other file (e.g. sys.stdin.read()), and you don't have to close it. stdin means "standard input".
Might be helpful if you read through this post, which seems to be similar to yours.
'stdin' in this case would be the argument on the command line coming after the python script, so python script.py input_file. This input_file would be the file containing whatever data you are working on.
So, you're probably wondering how to read stdin. There are a couple of options. The one suggested in the thread linked above goes as follows:
import fileinput
for line in fileinput.input():
#read data from file
There are other ways, of course, but I think I'll leave you to it. Check the linked post for more information.
Depending on the context of your assignment, stdin may be automatically sent into the script, or you may have to do it manually as detailed above.

Python read from command line arguments or stdin [duplicate]

This question already has answers here:
Read from File, or STDIN
(9 answers)
Closed 21 days ago.
When writing text-oriented command line programs in Python, I often want to read either all the files passed on the command line, or (XOR) standard input (like Unix cat does, or Perl's <>). So, I say
if len(args) == 0: # result from optparse
input = sys.stdin
else:
input = itertools.chain(*(open(a) for a in args))
Is this the Pythonic way of doing this, or did my miss some part of the library?
You need fileinput.
A standard use case is:
import fileinput
for line in fileinput.input():
process(line)
In Python 3, argparse handles filetype objects very nicely. It's an extremely powerful module and the docs come with many examples, so it's easy to quickly write the code you want. (How Pythonic!)
You may also benefit from this StackOverflow question about using argparse to optionally read from stdin.

Categories