I'm trying to create a console input program in which a user can add HTML to a file.
So if you pressed B(for bold)
and then your input was "test"
It would put "test" into the file.
The problem is, there is only one function "SaveToFile" that actually should be saving it to the file, and until that function is called, I'm expected to keep the output queued up somehow to later write onto the file, or to clear instead of putting into the file.
I know java and C# so my thought was to have some type of member variable
and just concatenate the lines:
mOutput += ("<B>"+userinput+"<B/>);
Any idea how I would achieve something like this in python?
Pretty much the same way as in the other languages. The differences are:
In a Python method, you always have to refer to a member variable (generally called instance attributes) by specifying the object it's attached to, even if it's your own instance. This is achieved by Python automatically passing a reference to your instance as the first argument to your method, which is by convention named self. So instead of mOutput you would probably write self.output (no Hungarian necessary since the self makes it obvious that it's a member variable).
The container you want to use for this is a list. You call the list's append method to add an item to it. Lists also support += but this expects a sequence, not a single item. Worse, strings are considered sequences of characters, so using += to append a string to a list would append the individual characters of the string as separate items! You could make the second argument a sequence by writing e.g. container += [item_to_add], but that forces Python to construct a second list for no good reason.
Instance attributes must be explicitly initialized at instantiation. If you define the attribute on the class, such as writing output = [] in the class definition, that attribute is shared among all instances of the class (unless the instances override it by providing an attribute with the same name). Instead, write self.output = [] in your __init__ method.
You can use the join method of strings to join a sequence together. The string specified is the delimiter between the joined elements. (This is backward from most languages, but makes sense after a while.)
Putting this all together:
class MyObject(object):
def __init__(self):
self.output = [] # empty list
def bold(self, userinput):
self.output.append("<B>" + userinput + "</B>")
def save(self, filename):
with open(filename, "w") as outfile:
# write items separated by line breaks
outfile.write("\n".join(self.output))
Or something like that.
Related
To clarify, i'm reading from a file and sending each line to a function(1) where it's relevant elements are put into a list. That list is then sent to another function(2) and added to a dictionary, with one element of that list being a key, and the other(s) put inside another list, being the value. So, basically {key:(value(,value)).
Problem is, whenever I send the list from (1) to (2), the newly created dictionary is overwritten. I'm new to Python, but i'm pretty sure I can add multiple keys and values to one dictionary right? So, is there a way to save the elements of the dictionary each time (2) is called? So, if it's called once, it has tokens(a) in the dictionary. When it's called again, it still has tokens(a), and now tokens(b) is added, and so forth.
If you need code I can include it.
MCVE:
def file_stuff(file name):
#opens and reads file using with x open as thing....then
for line in thing:
function1(line)
def function1(line):
#Breaks down line using regex; variable name var for this example
#list the relevant components of the line will go into
element = list()
for x in var:
list.append(x)
function2(element)
def function2(element):
#Another list is made along with a dictionary
webster = dict()
values = list()
for x in range(len(element)):
#inserts into dictionary.....the problem being, however, that I need this dictionary to "remember" what was already stored inside
In your current code, webster is a local variable in function2 that gets bound to a dictionary. However, you never return or do anything else with the dictionary that would allow code outside that function to see it, so when the function ends, there are no further references to the dictionary and it will be garbage collected.
If you want each call to function2 to use the same dictionary, you need to change the function so that it accesses the dictionary differently. Exactly what way is best will depend on the larger design of your program.
One option would be to make webster a global variable, which function2 can modify in place. This is very easy to do, but it has some pretty severe limitations, since a module has just the one global namespace. Working with multiple files that should have their data put into multiple different dictionaries would be very tough.
It would look something like this:
webster = {}
def function2(element):
...
webster[some_key] = some_value
Another option would be to make the dictionary an argument to the function, so that the calling code is responsible for creating and holding a reference to it in between calls. This is probably a better approach than using a global variable, but it's harder for me to demonstrate since it's not really clear to me where the dictionary should live in your example (maybe in function1, or maybe it needs to be passed all the way through from file_stuff).
It might look something like:
def caller():
the_dict = {}
for item in some_sequence():
function2(item, the_dict)
def function2(item, webster)
...
webster[some_key] = some_value
A final option would be to have function2 still be in charge of creating the dictionary, but for it to return the dictionary to its caller, who could do something with it (such as merging its contents with the dictionaries from previous calls). I'm not even going to attempt to demonstrate this one, since the merging process would depend a lot on what exactly you're putting in your dictionary. (A related option would be to return some other non-dictionary value (or a tuple of values) which could then be inserted in a dictionary by the calling code. This might be easier than dealing with an intermediate dictionary in some situations.)
I want to do matching my time-series data to meta data from a given file.
In my code, main function calls "create_match()" function every 1 minute. Inside "create_match(), there is a "list_from_file()" function to read data from file and store in lists to perform matching.
The problem is that my code is not effective since every 1 minute, it reads the file and rewrite in the same lists. I want to read file only one time (to initialize lists only one time), and after that ignoring the "list_from_file()" function. I do not want to just move this task to main function and pass lists through function.
Does python have a special variable like static variable in c programming?
Python does not have a static variable declaration; however, there are several fairly standard programming patterns in python to do something similar. The easiest way is to just use a global variable.
Global Variables
Define a global variable and check it before running your initialize function. If the global variable has already been set (ie. the lists you're reading in), just return them.
CACHE = None
def function():
global CACHE
if CACHE is None:
CACHE = initialize_function()
return CACHE
You can use a class:
class Match (object):
def __init__(self):
self.data = list_from_file()
def create_match(self):
# do something with `self.data` here
Make an instance:
match = Match()
This calls list_from_file().
Now, you can call create_match() repeatedly with access to self.data
import time
for x in range(10):
match.create_match()
time.sleep(60)
There are lots of ways.
You can make a variable part of a class - not a member of the object, but of the class itself. It is initialized when the class is defined.
Similarly you can put a variable at the outer level of a module. It will belong to the module, and will be initialed when the module is imported the first time.
Finally there's the hack of defining an object as a default parameter to a function. The variable will be initialized when the function is defined, and will belong to the function. You will only be able to access it with the parameter name, and it can be overridden by the caller.
Let's say I have the following string:
a<firstIndex>b<secondIndex>c<thirdIndex>
And and I want to replace all occurances of r'<\w+Index>' with a number that corresponds to the number of the match. So given the above string, the return value would be:
a1b2c3
I know there are lots of way to accomplish this in code (e.g. by writing a class with a counter that keeps track of the match index) but I'm wondering if this is possible through standard library functions alone.
I suppose, more specifically, I'm wondering if you can get this information from the MatchObject object passed to the repl function used in subn.
Read the docs for re.sub. re.sub (and re.subn) can take a function as the replacement parameter, not just a str. If you pass a function (really, any callable) as the repl, it is called for each match with the match object as the sole argument.
While the match object won't tell you how many matches have occurred, you can use the fact that it accepts any callable to make your own class that will track it for you. For example:
class Replacer: # On Py2, use class Replacer(object): to explicitly use new style classes
def __init__(self):
self.matchcnt = 0
def __call__(self, matchobj):
self.matchcnt += 1
return matchobj.group(0) + str(self.matchcnt)
When you call re.sub/re.subn, you construct and pass Replacer() as the repl; that initializes a new object with a fresh matchcnt, and since it defines __call__, it acts like a function with state; each time it matches and replaces, the object state is incremented so it will use the next number on the subsequent replace. When the sub finishes, the object is disposed of, and creating another one for a subsequent sub restarts the count.
I apologize for the newbie question, but this is my first time working with classes. The class I'm trying to create is intended to perform a regex find and replace on all keys and values within a dictionary. The specific find and replace is defined upon instantiation.
There are two issues that I have. The first issue is that each instance of the class needs to accept a new dictionary. I'm not clear on how to create a class that accepts a general dictionary which I can specify upon creating an instance.
The second issue is that the class I have simply isn't working. I'm receiving the error message TypeError: expected string or buffer in the class line v = re.sub(self.find,self.replace,v).
There are three instances I want to create, one for each input dictionary: input_iter1, input_iter2, and input_iter3.
The following is the class:
class findreplace:
values = []
keys = []
def __init__(self, find, replace):
self.find = find
self.replace = replace
def value(self):
for k,v in input_iter1.items():
v = re.sub(self.find,self.replace,v)
findreplace.values.append(v)
def key(self):
for k,v in input_iter1.items():
k = re.sub(self.find,self.replace,k)
findreplace.keys.append(k)
The following are the instances:
values1 = findreplace('[)?:(]','')
values1.value()
values2 = findreplace(r'(,\s)(,\s)(\d{5})({e<=1})',r'\2\3')
values2.value()
keys1 = findreplace(r'(?<=^)(.+)(?=$)',r'(?:\1)')
keys1.key()
keys2 = findreplace(r'(?=$)',r'{e}')
keys2.key()
print values
print keys
If anyone has any insight on how I can workaround these two issues, I'd be grateful to hear them. Thanks!
First, Python 2 classes should start off this way:
class Foo(object):
Otherwise, you get an "old-style class", which is some ancient crusty thing no one uses.
Also, class names in Python are typically written in CamelCase.
Second, do not use mutable values (like lists!) as class attributes, as you're doing here with keys and values. They'll be shared across all instances of your class! It looks like you're even aware of this, since you refer to findreplace.keys directly, but it doesn't make sense to store instance-specific values in a class attribute like that.
But, most importantly: why is this a class at all? What does a findreplace represent? It looks like this would be much clearer if it were just a single function.
To answer your actual questions:
You pass in a dictionary just like you're passing in find and replace. Add another argument to __init__, and pass another argument when you construct your class.
Presumably, you're getting the TypeError because one of the values in your dictionary isn't a string, and you can only perform regexes on strings.
Where is your definition of the input_iter dicts? How do they look like? Your error indicates that the values of your dicts are not strings.
I have a bunch of variables that are equal to values pulled from a database. Sometimes, the database doesn't have a value and returns "NoneType". I'm taking these variables and using them to build an XML file. When the variable is NoneType, it causes the XML value to read "None" rather than blank as I'd prefer.
My question is: Is there an efficient way to go through all the variables at once and search for a NoneType and, if found, turn it to a blank string?
ex.
from types import *
[Connection to database omitted]
color = database.color
size = database.size
shape = database.shape
name = database.name
... etc
I could obviously do something like this:
if type(color) is NoneType:
color = ""
but that would become tedious for the 15+ variables I have. Is there a more efficient way to go through and check each variable for it's type and then correct it, if necessary? Something like creating a function to do the check/correction and having an automated way of passing each variable through that function?
All the solutions given here will make your code shorter and less tedious, but if you really have a lot of variables I think you will appreciate this, since it won't make you add even a single extra character of code for each variable:
class NoneWrapper(object):
def __init__(self, wrapped):
self.wrapped = wrapped
def __getattr__(self, name):
value = getattr(self.wrapped, name)
if value is None:
return ''
else:
return value
mydb = NoneWrapper(database)
color = mydb.color
size = mydb.size
shape = mydb.shape
name = mydb.name
# All of these will be set to an empty string if their
# original value in the database is none
Edit
I thought it was obvious, but I keep forgetting it takes time until all the fun Python magickery becomes a second nature. :) So how NoneWrapper does its magic? It's very simple, really. Each python class can define some "special" methods names that are easy to identify, because they are always surrounded by two underscores from each side. The most common and well-known of these methods is __init__(), which initializes each instance of the class, but there are many other useful special methods, and one of them is __getattr__(). This method is called whenever someone tries to access an attribute. of an instance of your class, and you can customize it to customize attribute access.
What NoneWrapper does is to override getattr, so whenever someone tries to read an attribute of mydb (which is a NoneWrapper instance), it reads the attribute with the specified name from the wrapped object (in this case, database) and return it - unless it's value is None, in which case it returns an empty string.
I should add here that both object variables and methods are attributes, and, in fact, for Python they are essentially the same thing: all attributes are variables that could be changed, and methods just happen to be variables that have their value set to a function of special type (bound method). So you can also use getattr() to control access to functions, which could lead to many interesting uses.
The way I would do it, although I don't know if it is the best, would be to put the variables you want to check and then use a for statement to iterate through the list.
check_vars = [color,size,shape,name]
for var in check_vars:
if type(var) is NoneType:
var = ""
To add variables all you have to do is add them to the list.
If you're already getting them one at a time, it's not that much longer to write:
def none_to_blank(value):
if value is None:
return ""
return value
color = none_to_blank(database.color)
size = none_to_blank(database.size)
shape = none_to_blank(database.shape)
name = none_to_blank(database.name)
Incidentally, use of "import *" is generally discouraged. Import only what you're using.
you can simply use:
color = database.color or ""
another way is to use a function:
def filter_None(var):
"" if (a is None) else a
color = filter_None(database.color)
I don't know how the database object is structured but another solution is to modify the database object like:
def myget(self, varname):
value = self.__dict__[varname]
return "" if (value is None) else value
DataBase.myget = myget
database = DataBase(...)
[...]
color = database.myget("color")
you can do better using descriptors or properties