Is there a way to automate the creation of a new text file that contains a dictionary from a different file? - python-2.7

I'm using Python 2.7
Here I create a set of dictionaries:
day0 = 0
day1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25]
month0 = 0
december = [day0, day1]
calendar = [month0, december]
Then what I want to do is this:
file = open("calendarScript.py", "w")
file.write(calendar) ## Trying to create the calendar in a new doc
file.close()
But I get this error:
TypeError: expected a string or other character buffer object
Is there a way to recreate a dictionary in a new document?
Thank you for your help :)
P.s., I just tried this:
import shutil
shutil.copy(calendar, newFolder)
And got back this error:
TypeError: coercing to Unicode: need string or buffer, list found
Trying to find a way to copy a dict to a new file.

The answer to my problem was "dump". What I was trying to do was "dump" to a text file. Thanks to #KFL for this response (link below):
Writing a dict to txt file and reading it back?
>>> import json
>>> d = {"one":1, "two":2}
>>> json.dump(d, open("text.txt",'w'))
He also answered what was going to be my next problem:
>>> d2 = json.load(open("text.txt"))
>>> print d2
{u'two': 2, u'one': 1}

Related

Return the last k numbers of a list (Python) [duplicate]

I need the last 9 numbers of a list and I'm sure there is a way to do it with slicing, but I can't seem to get it. I can get the first 9 like this:
num_list[0:9]
You can use negative integers with the slicing operator for that. Here's an example using the python CLI interpreter:
>>> a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
>>> a[-9:]
[4, 5, 6, 7, 8, 9, 10, 11, 12]
the important line is a[-9:]
a negative index will count from the end of the list, so:
num_list[-9:]
Slicing
Python slicing is an incredibly fast operation, and it's a handy way to quickly access parts of your data.
Slice notation to get the last nine elements from a list (or any other sequence that supports it, like a string) would look like this:
num_list[-9:]
When I see this, I read the part in the brackets as "9th from the end, to the end." (Actually, I abbreviate it mentally as "-9, on")
Explanation:
The full notation is
sequence[start:stop:step]
But the colon is what tells Python you're giving it a slice and not a regular index. That's why the idiomatic way of copying lists in Python 2 is
list_copy = sequence[:]
And clearing them is with:
del my_list[:]
(Lists get list.copy and list.clear in Python 3.)
Give your slices a descriptive name!
You may find it useful to separate forming the slice from passing it to the list.__getitem__ method (that's what the square brackets do). Even if you're not new to it, it keeps your code more readable so that others that may have to read your code can more readily understand what you're doing.
However, you can't just assign some integers separated by colons to a variable. You need to use the slice object:
last_nine_slice = slice(-9, None)
The second argument, None, is required, so that the first argument is interpreted as the start argument otherwise it would be the stop argument.
You can then pass the slice object to your sequence:
>>> list(range(100))[last_nine_slice]
[91, 92, 93, 94, 95, 96, 97, 98, 99]
islice
islice from the itertools module is another possibly performant way to get this. islice doesn't take negative arguments, so ideally your iterable has a __reversed__ special method - which list does have - so you must first pass your list (or iterable with __reversed__) to reversed.
>>> from itertools import islice
>>> islice(reversed(range(100)), 0, 9)
<itertools.islice object at 0xffeb87fc>
islice allows for lazy evaluation of the data pipeline, so to materialize the data, pass it to a constructor (like list):
>>> list(islice(reversed(range(100)), 0, 9))
[99, 98, 97, 96, 95, 94, 93, 92, 91]
The last 9 elements can be read from left to right using numlist[-9:], or from right to left using numlist[:-10:-1], as you want.
>>> a=range(17)
>>> print a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
>>> print a[-9:]
[8, 9, 10, 11, 12, 13, 14, 15, 16]
>>> print a[:-10:-1]
[16, 15, 14, 13, 12, 11, 10, 9, 8]
Here are several options for getting the "tail" items of an iterable:
Given
n = 9
iterable = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Desired Output
[2, 3, 4, 5, 6, 7, 8, 9, 10]
Code
We get the latter output using any of the following options:
from collections import deque
import itertools
import more_itertools
# A: Slicing
iterable[-n:]
# B: Implement an itertools recipe
def tail(n, iterable):
"""Return an iterator over the last *n* items of *iterable*.
>>> t = tail(3, 'ABCDEFG')
>>> list(t)
['E', 'F', 'G']
"""
return iter(deque(iterable, maxlen=n))
list(tail(n, iterable))
# C: Use an implemented recipe, via more_itertools
list(more_itertools.tail(n, iterable))
# D: islice, via itertools
list(itertools.islice(iterable, len(iterable)-n, None))
# E: Negative islice, via more_itertools
list(more_itertools.islice_extended(iterable, -n, None))
Details
A. Traditional Python slicing is inherent to the language. This option works with sequences such as strings, lists and tuples. However, this kind of slicing does not work on iterators, e.g. iter(iterable).
B. An itertools recipe. It is generalized to work on any iterable and resolves the iterator issue in the last solution. This recipe must be implemented manually as it is not officially included in the itertools module.
C. Many recipes, including the latter tool (B), have been conveniently implemented in third party packages. Installing and importing these these libraries obviates manual implementation. One of these libraries is called more_itertools (install via > pip install more-itertools); see more_itertools.tail.
D. A member of the itertools library. Note, itertools.islice does not support negative slicing.
E. Another tool is implemented in more_itertools that generalizes itertools.islice to support negative slicing; see more_itertools.islice_extended.
Which one do I use?
It depends. In most cases, slicing (option A, as mentioned in other answers) is most simple option as it built into the language and supports most iterable types. For more general iterators, use any of the remaining options. Note, options C and E require installing a third-party library, which some users may find useful.

Getting the ID of the Max record in an aggregate

Take these models:
class Rocket(Model):
...
class Flight(Model):
rocket = ForeignKey(Rocket)
start_time = DateTimeField(...)
If I want to get start times of the latest flight for every rocket, that is simple:
>>> Flight.objects.values('rocket').annotate(max_start_time=Max('start_time'))
<QuerySet [
{'rocket': 3, 'max_start_time': datetime.datetime(2019, 6, 13, 6, 58, 46, 299013, tzinfo=<UTC>)},
{'rocket': 4, 'max_start_time': datetime.datetime(2019, 6, 13, 6, 59, 12, 759964, tzinfo=<UTC>)},
...]>
But what if instead of max_start_time I wanted to select IDs of those same Flights?
In other words, I want to get the ID of the latest Flight for every rocket.
What database backend are you using? If your database backend has support for DISTINCT ON this is most easily accomplished by:
Flight.objects.order_by("rocket", "-start_time").distinct("rocket").values("id", "rocket")

"how to fix MathJax linebreaking?"

I'm using doubleslash(\\) for line-breaking ,the cursor is pointing to the next line but a single slash(\) is appending with my data.
This is the input I am giving:
Find the median of the given data:"\\ "13, 16, 12, 14, 19, 12, 14, 13, 14"
The output is:
Find the median of the given data: \13, 16, 12, 14, 19, 12, 14, 13, 14.
Single slash is appending to the data.
Try using \\\\. Your content management system may be using \ as a special character, an that may turn \\ into \ in the resulting HTML. For example, Markdown usually does that.

Django time convert format

I am trying to convert time into specific format using following code:
datetime.datetime.strptime(timezone.now() + timedelta(days=14), '%Y-%m-%dT%H:%M:%S.%f%z')
I am getting error that the first parameter has to be string.
I also tried using this code
datetime.datetime.strptime(str(timezone.now() + timedelta(days=14)), '%Y-%m-%dT%H:%M:%S.%f%z')
But then I am getting following error:
time data '2017-06-04 14:26:18.941458+00:00' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'
Can someone suggest how can I convert to the specific format. Thanks!
Django has a built in date parser that abstracts the logic of strftime away called parse_datetime. You can simply pass in a date string and it will return a datetime.datetime object. Here's what your code would look like:
from django.utils.dateparse import parse_datetime
parse_datetime(str(timezone.now() + timedelta(days=14)))
>>> datetime.datetime(2017, 6, 4, 7, 45, 28, 301957)
If you are trying to convert the datetime.datetime() object into string, you could just do this,
>>> datetime.datetime.strftime(timezone.now() + timedelta(days=14), '%Y-%m-%dT%H:%M:%S.%f%z')
'2017-06-04T14:45:23.621658+0000'
Or if you want it as the datetime object,
>>> datetime.datetime.strptime(str(timezone.now() + timedelta(days=14))[:26], '%Y-%m-%d %H:%M:%S.%f')
datetime.datetime(2017, 6, 4, 14, 50, 26, 5649)

To change the node's name to have a unique list python

I have a problem. Should I create a series of graphs with the barabasi_albert_graph function that is in the library called NetworkX in Python; I should bring all nodes of the graph in a list but so that if I have two graphs one with 5 nodes and one with 10 nodes. I would like to have a unique list so made [0,1,2,3,4,5,6,7 , 8,9,10,11,12,13,14]. The first 5 represent those of the first graph and the 10 the others. Instead I find having [0,1,2,3,4,0,1,2,3,4,5,6,7,8,9]. How can I do to have the first list so that for example if I write 11 in G gives me True?
You can change the labels using convert_node_labels_to_integers like this
In [1]: import networkx as nx
In [2]: G = nx.barabasi_albert_graph(10,3)
In [3]: H = nx.convert_node_labels_to_integers(G,first_label=100)
In [4]: G.nodes()
Out[4]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [5]: H.nodes()
Out[5]: [100, 101, 102, 103, 104, 105, 106, 107, 108, 109]