Tags: python tuples

Add Variables to Tuple

Tags: ,


I am learning Python and creating a database connection. While trying to add to the DB, I am thinking of creating tuples out of information and then add them to the DB. What I am Doing: I am taking ...

Python dicts in sqlalchemy

Tags: , , ,


I would like to load/save a dict to/from my sqlite DB, but am having some problems figuring out a simple way to do it. I don't really need to be able to filter, etc., based on the contents so a simple ...

Determine if Python variable is an instance of a built-in type

Tags: , ,


I need to determine if a given Python variable is an instance of native type: str, int, float, bool, list, dict and so on. Is there elegant way to doing it? Or is this the only way: if myvar in (...

Tags: python

Is it safe to replace a self object by another object of the same type in a method?

Tags:


I would like to replace an object instance by another instance inside a method like this: class A: def method1(self): self = func(self) The object is retrieved from a database.

Using module ‘subprocess’ with timeout

Tags: , , ,


Here's the Python code to run an arbitrary command returning its stdout data, or raise an exception on non-zero exit codes: proc = subprocess.Popen( cmd, stderr=subprocess.STDOUT, # Merge ...

Tags: python

What is the best way to call a script from another script?

Tags:


I have a script named test1.py which is not in a module. It just has code that should execute when the script itself is run. There are no functions, classes, methods, etc. I have another script which runs as a service. I want to call test1.py from the script running as a service. For example: File test1.py File service.py I’m aware of one method which is opening the file, reading the contents, and basically eval’ing it. I’m assuming there’s a better way of doing this. Or at least I hope so. Answer The usual way to do this is something

Equivalent of NotImplementedError for fields in Python

Tags: ,


In Python 2.x when you want to mark a method as abstract, you can define it like so: class Base: def foo(self): raise NotImplementedError("Subclasses should implement this!") Then if you ...

python list comprehensions; compressing a list of lists?

Tags: , ,


guys. I'm trying to find the most elegant solution to a problem and wondered if python has anything built-in for what I'm trying to do. What I'm doing is this. I have a list, A, and I have a ...

Get the index of an element in a queryset

Tags: , , ,


I have a QuerySet, let's call it qs, which is ordered by some attribute which is irrelevant to this problem. Then I have an object, let's call it obj. Now I'd like to know at what index obj has in qs,...

Shuffling a list of objects

Tags: , , ,


I have a list of objects and I want to shuffle them. I thought I could use the random.shuffle method, but this seems to fail when the list is of objects. Is there a method for shuffling objects or ...