Tagged Questions
0
votes
0answers
35 views
Usage of @property [on hold]
In many Python projects I see tendency to not using @property decorator for methods that, for me, would make a lot sense. Here are examples from Django:
form.errors.as_json()
form.is_valid()
Since ...
0
votes
1answer
36 views
how can I object-orient a simple website copying app?
Getting straight to the point, I am new to object oriented programming, while learning Python, I wrote a simple procedural program that accepts a URL and fetch the data from the URL to a text file.
...
0
votes
0answers
17 views
How to create an interface between modules (Python)
I've created two Python "pieces" to be used in an embedded system:
A module which sends and receives messages over the cell phone network. This is a called "GSMMessenger"
A function that processes ...
0
votes
0answers
42 views
To Singleton or not to Singleton for batch process
Before anything, sorry for the broad title and hello StackOverflow !
I don't mean to start a flamewar or a politics discussion over here.
Here's my doubt, I'm working in a django project in which ...
0
votes
2answers
27 views
Run some method at start of banch of methods (python)
May be I'm doing wrong way, so tell me how to do it better.
I'm implementing one class for all instances of some object, say users. (looks like it is pattern 'Table Module' in M. Fowler's "Patterns of ...
0
votes
0answers
31 views
Design a one-time functional class
Imagine a class Interpreter that takes a Parser and evaluates the data yielded by the Parser into a Document. The Interpreter must be instantiated and is supposed to be executed immediately, with no ...
3
votes
3answers
102 views
Is there a pattern for one use only objects?
Suppose you are representing some task to do using objects of a class Task2Do. These objects are runnable, that is, they have a method which perform a task doTask.
On the other hand, you have a queue ...
1
vote
2answers
44 views
Inheriting a python class while adding a property
I'm running into a problem trying to inherit a python class while adding a property. To illustrate, define the following classes:
class A:
def __init__(self, prop, x1, x2):
self.prop = ...
-3
votes
1answer
30 views
Choosing the right design pattern for execution on a dictionary key
Which design pattern is preferred?
For a dictionary d that may or may not have a key named 'foo'...
Pattern A
if d.get('foo'):
func(d.get('foo'))
Pattern B
foo = d.get('foo')
if foo:
...
1
vote
1answer
19 views
Multiple DAL design pattern
I am relatively new to python. I am building a system that will initially fetch data from a database, but at some point in the future, we’ll be getting the data from a service. To account for this I ...
0
votes
0answers
39 views
Pattern to review while writing class
I am writing my base class to handle all restful API (using cornice under it) for my project.
e.g.: a common GET method that -by default- will do:
results = ...
0
votes
0answers
22 views
Should a model for a BlogPost know about different representations and database lookup?
I'm creating a blogging engine in Pyramid and MongoDB. The idea is posts are written and edited in Markdown and only displayed in HTML. Post lookups are done by an automatically generated "web title" ...
4
votes
1answer
54 views
Is there a standard approach to returning values from coroutine endpoints
My question:
I would like to know if there is a "best practice" pattern in Python for returning values from coroutine endpoints (aka the "sink" or "consumer"). More generally, how would you approach ...
6
votes
3answers
79 views
Method overloading for different argument type in python
I'm writing a preprocessor in python, part of which works with an AST.
There is a render() method that takes care of converting various statements to source code.
Now, I have it like this ...
0
votes
1answer
40 views
Uncertain how to design django apps - best practices
In every django app I've previously developed I used only one app, which had all models, forms etc included.
It was OK for small apps, but now it's time to do it right way :)
I want to have auth ...
0
votes
0answers
29 views
Python: reimport source code after catching exception
I recently encounter the following situation in writing Python script:
I have an controller instance that runs a long-lasting, heavy data analysis job. The job is sophisticated enough that I miss ...
0
votes
3answers
37 views
Python: Try three times a function until all failed
I am writing in Python 2.7 and encounter the following situation. I would like to try calling a function three times. If all three times raise errors, I will raise the last error I get. If any one of ...
1
vote
1answer
62 views
Design pattern for combining objects
I have a big ugly function that I would like to refactor.
Schematically, the function takes 2 objects, accesses several
attributes and methods on these objects, combine them and use them and
...
0
votes
1answer
36 views
python recursive regular expression
I have a message that is
msg = 'untagged ethernet 1 ethernet 2 ethernet 3'
and I want to write a regex that will find the 'ethernet x' pattern so that if i run
m = re.match(str(regex),msg)
print ...
0
votes
1answer
35 views
Passing the *class* as function argument
I've never done this before, and it seems hacky, but I've tested and it does work. Is this considered OK form:
from audio.models import audio
from document.models import pdf
def ...
1
vote
1answer
53 views
Best practice for two classes with a relationship
I'm getting used to Python's packaging mechanisms, and working to understand how to avoid circular imports. It seems difficult to develop a class based model with relationships between the classes ...
0
votes
1answer
33 views
Python import loops and top level program structure
So to help me learn Python I'm building a simple little text based game. I'm working on the rough highest level structure of the files. So far I have main.py, input.py and commands.py.
main.py
from ...
3
votes
2answers
46 views
Factory method pattern conflicts with use of multiprocessing queue
I have implemented the factory method pattern to parametrize the base class of the product class:
def factory(ParentClass):
class Wrapper(ParentClass):
_attr = "foo"
def ...
0
votes
0answers
33 views
Should I access these attributes directly or rather use proxy methods?
My client API encapsulates connections to the server in a class ServerConnection that stores an asyncio.StreamReader/-Writer pair. (For simplicity, I will not use yield from or any other async ...
-2
votes
1answer
32 views
Python subclassing: adding properties
I have several classes where I want to add a single property to each class (its md5 hash value) and calculate that hash value when initializing objects of that class, but otherwise maintain everything ...
0
votes
2answers
78 views
Single Responsibility Principle for CRUD operations against a collection
I'm trying to understand how to logically separate CRUD responsibilities so to adhere to the Single Responsibility Principle (SRP).
As I understand the definition of SRP, a single responsibility ...
0
votes
0answers
24 views
Need to build regex pattern python [duplicate]
I have a bunch of terms in a list. I would like to take a string and perform a reg search on it by looping through the list and building the regex pattern.
import re
list = ['time', 'fetch', 'breathe ...
0
votes
0answers
50 views
Condition is right, class still not appending/removing - Python
I was coding my own version of Lucky 9 (each player gets 2 cards and is asked if they want another, their score is equivalent to (sum of the values)%10 ), when I encountered this problem.
Each player ...
-1
votes
1answer
41 views
How to share behavior b/w classes in python?
Like in ruby we have module which can be included in any given class and then we can use the methods defined there, this saves us from inheritance which is considered as a coupling. In python is there ...
3
votes
3answers
109 views
How can I call a sequence of functions until the return value meets some condition?
Sometimes I find myself writing code like this:
def analyse(somedata):
result = bestapproach(somedata)
if result:
return result
else:
result = notasgood(somedata)
...
0
votes
1answer
35 views
Design Pattern for logging in Multi threaded system
How can we make use of design pattern for log generation in Multi threaded environment. There is one log file and there are multiple threads need to write in this log file. So there has to be a ...
0
votes
0answers
36 views
sqlalchemy.Column __set__ method override
I have database that accessed by mean of sqlalchemy library. For example one of tables represent such that:
@as_declarative()
class Base(object):
id = Column(Integer, primary_key=True)
...
0
votes
2answers
23 views
File/Class pattern to use for a CLI program
Here's the thing, I want to develop a CLI program (in Python I think) and I'm a really beginner (Also is there a good IDE for Python?) in Python so I would like some of your knowledge to help figure ...
0
votes
0answers
28 views
Navigation of nested datastructures containing differing classes
I have a data structure of nested dicts and lists in my application (code below).
The data structure is held by Import objects which are in turn held by the imports attribute of the application.
So ...
2
votes
0answers
38 views
Handling multiple inheritance w/ different call signs
So this is kind of a python design question + multiple heritance. I'm working on a program of mine and I've ran into an issue I can't figure out a decent way of solving.
To keep it simple. The ...
0
votes
1answer
58 views
Any pattern to remove these if else?
# status.py
class Status(object):
@classmethod
def add(cls, title, kind, attachment):
self.db.set('title', title)
self.db.set('kind', kind)
self.db.set('attachment', ...
0
votes
0answers
54 views
Python - use of globals() vs. code readability
I'm working on a solid implementation of the state design pattern in python. In doing so I've found two ways of doing things.
Way 1
class State(object):
transitions = [(None, None, None)]
...
0
votes
1answer
14 views
DRYly represent a list of settings objects
I'm sketching out a Django model that has a few different boolean fields:
video_enabled = models.BooleanField()
audio_enabled = models.BooleanField()
sensors_enabled = models.BooleanField()
...
1
vote
0answers
29 views
Python C API: initialize instance member objects to NULL or None?
When creating a new instance of a custom class in the C API, is it preferable to initialize the member variables to NULL or None?
5
votes
5answers
74 views
Finding equality between different strings that should be equal
I have data about soccer teams from three different sources. However, the 'team name' for the same team from each of these sources differ in style.
For e.g.
[Source1] [Source2] ...
0
votes
1answer
38 views
Python Adaptor pattern
Say I have the following class
class MultiplePeopleInBook(object):
def __init__(self, names, ages, book_title):
self._names = names
self._ages = ages
self._book_title = ...
0
votes
2answers
35 views
How to encapsulate different methods in a class under different names in python?
So I am currently trying to develop a test framework in Python and I have developed a bunch of API methods to be used for this. This framework is to be used by number of different people with ...
5
votes
7answers
125 views
Method Refactor: from many kwargs to one arg-object
Sometimes the number of kwargs of a method increase to a level where I think it should be refactored.
Example:
def foo(important=False, debug=False, dry_run=False, ...):
....
...
4
votes
3answers
226 views
Pre-allocating a list of None
Suppose you want to write a function which yields a list of objects, and you know in advance the length n of such list.
In python the list supports indexed access in O(1), so it is arguably a good ...
0
votes
2answers
44 views
Remove line pattern script in Python not fully functionnal
For logs parsing, I need to remove a pattern in a log file :
Pattern :
Acces : Lecture donnees (ou liste de repertoire) Privileges : - Nombre de SID restreint : 0 Masque d acces : 0x1 "
Log sample ...
0
votes
0answers
39 views
Pattern Search inside 999 .tfa files with the help of python
I have about 999 temporary fastA files(basically text files) in a folder. I would like to implement Python to search for definitive Pattern inside those files. The Program should basically go through ...
0
votes
1answer
79 views
How to output data in python scripts?
I'm developing a CLI tool (with python) running in two different modes, the first mode is the classic one where the output is plain text (human readable), and the second mode (activated via a --gui ...
2
votes
5answers
70 views
module with classes with only static methods
I have a Python module that contains a number of classes, each representing a particular physical material with its properties (e.g., density, specific heat). Some of the properties are just float ...
0
votes
1answer
66 views
empty function object in python
I've heard that python functions are objects, similar to lists or dictionaries, etc. However, what would be a similar way of performing this type of action with a function?
# Assigning empty list to ...
0
votes
0answers
36 views
Implementing a high level 'interface' class [duplicate]
Say I have a few 'low-level' classes that implement interaction with different hardware devices or other software components.
I want to provide a high-level 'interface' class. This class would ...