Does anyone know of a memory efficient way to generate very large xml files (e.g. 100-500 MiB) in Python?
I've been utilizing lxml, but memory usage is through the roof.
What's the best way of getting the last item from an iterator in Python 2.6? For example, say
my_iter = iter(range(5))
What is the shortest-code / cleanest way of getting 4 from my_iter?
I could do this, but it doesn't seem very efficient:
[x for x in my_iter][-1]
Hi,
Fastest way to uniqify a list in Python without preserving order? I saw many complicated solutions on Internet - could they be faster then simply:
list(set([a,b,c,a]))
?
The greenlet package is used by gevent and eventlet for asynchronous IO. It is written as a C-extension and therefore doesn't work with Jython or IronPython. If performance is of no concern, what is the easiest approach to implementing the greenlet API in pure Python.
A simple example:
def test1():
print 12
gr2.switch()
print 34
def test2():
print 56
gr1.switch()
print 78
gr1 = greenlet(test1)
gr2 = greenlet(test2)
gr1.switch()
Should print 12, 56, 34 (and not 78).
Format is like:
CHINA;2002-06-25 00:00:00.000;5,60
CHINA;2002-06-26 00:00:00.000;5,32
CHINA;2002-06-27 00:00:00.000;5,31
and I try to use Python's CSV tools to parse it but cannot understand the paragraph, source:
And while the module doesn’t directly support parsing strings, it can easily be done:
import csv
for row in csv.reader(['one,two,three']):
print row
Could someone clarify the line ['one,two,three']? How would you use it with format A;B;C?
I have a question about idioms and readability, and there seems to be a clash of Python philosophies for this particular case:
I want to build dictionary A from dictionary B. If a specific key does not exist in B, then do nothing and continue on.
Which way is better?
try:
A["blah"] = B["blah"]
except KeyError:
pass
or
if "blah" in B:
A["blah"] = B["blah"]
"Do and ask for forgiveness" vs. "simplicity and explicitness".
Which is better and why?
I am pretty new to python and I discovered by myself that I can create a list of function and call with a for loop.
example:
def a(args):
print "A"
def b(args):
print "B"
def c(args):
print "C " + str(args)
functions = [a,b,c]
for i in functions:
i(1)
So, my question is: is there any good practice or elegant way to use list of functions and what is a good use of all this? (do have a particular name the "list of functions"?)
thank you
Hello everyone, i'm looking for a way in python to run an external binary and watch it's output for: "up to date" If "up to date" isn't returned i want to run the original command again, once "up to date" is displayed i would like to be able to run another script. So far I've figured out how to run the binary with options using subprocess but thats as far as I've gotten. Thanks!
Hi
I wrote a matlab code (that easily could be implimented as a function) that convert a series of BMPs to avi. I want a python program to call to this program/function. how do I do it?
thanks
Hey, here's a dumb question: how can I set an object property given its name in a string. I have a dictionary being passed to me and I wish to transfer its values into namesake properties using code like this:
for entry in src_dict:
if entry.startswith('can_'):
tgt_obj[entry] = src_dict_profile[entry]
I'm still a bit of a noob with Python so would appreciate some help.
- dave.
Hi,
I need to compare two files and redirect the different lines to third file. I know using diff command i can get the difference . But, is there any way of doing it in python ? Any sample code will be helpful
Hi
I am trying to perform a 2d convolution in python using numpy
I have a 2d array as follows with kernel H_r for the rows and H_c for the columns
data = np.zeros((nr, nc), dtype=np.float32)
#fill array with some data here then convolve
for r in range(nr):
data[r,:] = np.convolve(data[r,:], H_r, 'same')
for c in range(nc):
data[:,c] = np.convolve(data[:,c], H_c, 'same')
It does not produce the output that I was expecting, does this code look OK
Thanks
I'm looking for a way in python to find out which type of file system is being used for a given path. I'm wanting to do this in a cross platform way. On linux I could just grab the output of df -T but that won't work on OSX or windows.
Is there some relatively simple way to programmatically include source code lines to python logger report. For example...
import logging
def main():
something_is_not_right = True
logging.basicConfig(level=logging.DEBUG,
format=('%(filename)s: '
'%(levelname)s: '
'%(funcName)s(): '
'%(lineno)d:\t'
'%(message)s')
)
if something_is_not_right == True:
logging.debug('some way to get previous line of source code here?')
So that output would look like this.
example.py: DEBUG: main(): 14: if something_is_not_right == True:
Hi folks,
I'm having quite a problem deciding how to serve a few Python scripts.
The problem is that the basic functionality could be generalized by this:
do_something()
time.sleep(3)
do_something()
I tried various WSGI servers, but they have all been giving me concurrency limitations, as in I have to specify how many threads to use and so on.
I only wish that the resources on the server be used efficiently and liberally.
Any ideas?
I downloaded a webpage in my python script.
In most cases, this works fine.
However, this one had a response header: GZIP encoding, and when I tried to print the source code of this web page, it had all symbols in my putty.
How do decode this to regular text?
I am creating an application that lets users login using Google, Facebook and the website's native login. The site is being built in Python / Django.
What would be the best way to handle login, session management and user authentication?
I do not want to use the in-built Django user management. I am using Django very sparingly(URLs, templates)
Given a PyObject* pointing to a python object, how do I invoke one of the object methods? The documentation never gives an example of this:
PyObject* obj = ....
PyObject* args = Py_BuildValue("(s)", "An arg");
PyObject* method = PyWHATGOESHERE(obj, "foo");
PyObject* ret = PyWHATGOESHERE(obj, method, args);
if (!ret) {
// check error...
}
This would be the equivalent of
>>> ret = obj.foo("An arg")
I'm trying to use Emacs as a python editor and it works fine when I evaluate(C-c C-c) only single files but when I evaluate a file that imports another file in the same directory, I get an error saying that the file could not be imported.
Does anyone know of a workaround?
Thanks in advance
Hi,
I am planning to write a generic python module for installing a package. This script should retrieve the module from a remote machine or locally and install it on a given host and user. However, there needs to be changes made to the package files based on the host, user and given environment.
My approach is to use XML to describe changes to be made to package files based on environment. It will first extract the package to the user directory and then using an xml configuration file, it should replace the file values in the package directory. The xml would look something like this:
<package version="1.3.3">
<environment type="prod">
<file dir="d1/d2" name="f1">
<var id="RECV_HOST" value="santo">
<var id="RECV_PORT" value="RECV_PORT_SERVICE" type="service">
<var id="JEPL_SERVICE_NAME" value="val_omgact">
</file>
<var dir="d4/d3/s2" name="f2">
<var id="PRECISION" value="true">
<var id="SEND_STATUS_CODE" value="323">
<var id="JEPL_SERVICE_NAME" value="val_omgact">
</file>
</environment>
<environment type="qa">
<file dir="d1/d2" name="f1">
<var id="RECV_HOST" value="test">
<var id="RECV_PORT" value="1444">
<var id="JEPL_SERVICE_NAME" value="val_tsdd">
</file>
<file dir="d4/d3/s2" name="f2">
<var id="PRECISION" value="false">
<var id="SEND_STATUS_CODE" value="323">
<var id="JEPL_SERVICE_NAME" value="val_dsd">
</file>
</environment>
</package>
What are your thoughts on this approach? Is there an existing python module, package or script that I could use for this purpose since this seems fairly generic and can be used for any installation.
Thanks!
Sam
I have a string of HTML stored in a database. Unfortunately it contains characters such as ®
I want to replace these characters by their HTML equivalent, either in the DB itself or using a Find Replace in my Python / Django code.
Any suggestions on how I can do this?
I'd like to work with a dict in python, but limit the number of key/value pairs to X. In other words, if the dict is currently storing X key/value pairs and I perform an insertion, I would like one of the existing pairs to be dropped. It would be nice if it was the least recently inserted/accesses key but that's not completely necessary.
If this exists in the standard library please save me some time and point it out!