But why do I bring up the negative connotation on geek and nerd? Shouldn't I just say why they should write? Well it's because the reason that there is that aggressive tone towards the geek comes from their lack of being able to write properly. I'm sure that a geek is able to impress any academic with their verbose prose but to the everyday citizen, nothing truly resonates. And since it doesn't resonate, there is no relevance to them. But a geek is wrapped up in their subject and is desperately trying to inform others about what they know. By doing so, either one or the other starts to become aggressive. The geek for not getting through to the citizen and the citizen being annoyed with this supposed whack-job bombarding them with nonsense.
Geeks need to build the bridge between their knowledge and others, and in order to do that, they need to communicate properly. They have to find connections between their audience and their subject so that the audience becomes aware of what they have done and can appreciate all their effort. It may be easier for a geek to speak on the level of one versed in their jargon but not everyone who knows their same language will be able to give them the opportunities to become better. So they have to practice a multitude of ways of saying the same thing for whomever wants to know about what they're working on. That, in turn, removes the foreign and obnoxious nature that geek often invokes.
########################################################
To change the subject, I've decided that for the first two weeks to create a class to practice my programming skills, as well as attempt to explain the idea behind said class.
I've decided that I want to create a simple representation of a file cabinet containing files and safe files. A file is any item that Python can contain in a list or tuple. A safe file is an object that is a tuple that contains 2 items, with the first item (index 0) containing a password (which must be a string) and the second item (index 1) containing the file. First, let's initialize it.
class Cabinet:
""" An object of the bureaucracy """
def __init__(self):
""" (Cabinet) -> NoneType
Initialize new cabinet self to contain an empty list files
>>> c = Cabinet()
>>> c.files
[]
"""
self.files = []
I've made it so that when one creates a Cabinet, it will begin empty. This is so that it acts like a file cabinet as they usually do not contain any items inside them (unless one steals it from someone else). So in order to add items to Cabinet, I create a method dubbed file_away.
def file_away(self, file):
""" (Cabinet, file) -> NoneType
Store a file in self
>>> c = Cabinet()
>>> c.file_away('f')
>>> c.files
['f']
"""
self.files.append(file)
So now we can add files to the cabinet. But what about a safe file? That has a different set of parameters which you can see with the safe_file method.
def safe_file(self, file, code):
""" (Cabinet, file, str) -> NoneType
Precondition: code must be unique for every file
Store a safe file in self
>>> c = Cabinet()
>>> c.safe_file('f', '059401')
>>> c.files
[('059401', 'f')]
"""
if isinstance(code, str):
self.files.append((code, file))
Notice how safe_file converts the code and file into a tuple if and only if the code is a string. One thing I haven't included is an error exception so that if the user does not put an item that isn't a string, a message comes up. Though since the docstring already indicates that the code parameter has to be string, checking to see if code is a string is somewhat pointless.
Now you might be wondering what makes a safe_file special beyond its format. Well for that, I need to bring up the __str__ and __repr__ methods.
def __str__(self):
""" (Cabinet) -> str
Return a string version of self
>>> c = Cabinet()
>>> c.file_away(5001)
>>> c.file_away(['1', '6'])
>>> c.safe_file('f', '059401')
>>> print(c)
Cabinet:
[5001, ['1', '6']]
1 safe file(s)
"""
files = []
safe_files = 0
for i in self.files:
if isinstance(i, tuple) and len(i) == 2:
safe_files = safe_files + 1
else:
files.append(i)
return 'Cabinet: \n{0}\n{1} safe file(s)'.format(files, safe_files)
def __repr__(self):
""" (Cabinet) -> str
Return a shell representation of self
>>> c = Cabinet()
>>> c.file_away(5001)
>>> c.file_away(['1', '6'])
>>> c.safe_file('f', '059401')
>>> c
Cabinet([5001, ['1', '6']])
"""
files = []
safe_files = 0
for i in self.files:
if not (isinstance(i, tuple) and len(i) == 2):
files.append(i)
return 'Cabinet({})'.format(files)
Notice how the safe files are omitted from the representation and are only shown in amount when Cabinet is turned into a string. That is to ensure their safety from others who wish to access the file. It creates a semblance of privacy. Naturally, there is a way to get around that by seeing all of the files with self.files, which reveals both the file and the code to access the file, but you'll see soon why code has a purpose.
Creating a Cabinet, we can now create a method, file_search, to see whether or not we can find a file.
def file_search(self, file):
""" (Cabinet, file) -> index/str
Precondition: file must not be a tuple of len 2
Return the index that the file is in, provided it exists in self
>>> c = Cabinet()
>>> c.file_away(1)
>>> c.file_away(2)
>>> c.file_away(3)
>>> c.file_search(2)
1
>>> c.file_search(4)
'That file does not exist'
"""
for i in range(len(self.files)):
if self.files[i] == file:
return i
return 'That file does not exist'
To the astute reader, one could tell that file_search uses linear search, which while serviceable, may be slower for when Cabinet contains various files. I have also created a message for if a file cannot be located within the Cabinet. Be aware that in file_search's docstring, there is a precondition which is "forbidding" the search of a safe file. One could look for it since I have not written any code that explicitly prohibits you from accessing it through file_search, but to humor me, why don't we try looking at this next method, reveal_safe_file?
def reveal_safe_file(self, code):
""" (Cabinet, str) -> file/str
Return file by using code as an attempt
>>> c = Cabinet()
>>> c.safe_file('shh...', '1-1-3-1')
>>> c.safe_file(42, 'meaning_of_life')
>>> c.reveal_safe_file('meaning_of_life')
42
>>> c.reveal_safe_file('qwertyasdfzxcv')
'No file with that code exists'
"""
safe_files = []
for i in self.files:
if isinstance(i, tuple) and len(i) == 2:
safe_files.append(i)
for file in safe_files:
if file[0] == code:
return file[1]
return 'No file with that code exists'
This is a slightly more complicated method to follow. First, I append all safe files to a list called safe_files. Then within that list, I search through the first indexes of each of those safe_files to see if one matches with code. If they do, I return the file which is in the second index. But if not, I simply return that there is no file with that code in the Cabinet. This is where code comes into use and would be the ideal way a user could access a safe file.
This function is of course very rudimentary. There are still a variety of concepts I have missed such as:
- __eq__ method to check if two Cabinets are the same
- Creating methods like steal_files and copy_files to move/copy files from one Cabinet to another.
- Creating methods like shift_files and switch_files to move files within a Cabinet
- Creating restrictions in file_search so that searching for safe files would be prohibited
- Being able to remove a file from Cabinet
- Being able to sort Cabinet by a variety of ways that suit me
- Checking if there are two of the same file
- Ensuring that each code is unique to a designated file regardless of what the file contains
And probably various others. Still, I feel it's good practice for now and perhaps I may build on it in the future. For now, here is the completed class:
'''
A file is any item that Python can contain in a list or tuple
A safe file is an item that is a tuple with len 2, with index 0 containing
a password (code) and index 1 containing the file. Safe files can only
be accessed using reveal_safe_file to open the file and self.files for
docstring/moderator purposes
'''
class Cabinet:
""" An object of the bureaucracy """
def __init__(self):
""" (Cabinet) -> NoneType
Initialize new cabinet self to contain an empty list files
>>> c = Cabinet()
>>> c.files
[]
"""
self.files = []
def file_away(self, file):
""" (Cabinet, file) -> NoneType
Store a file in self
>>> c = Cabinet()
>>> c.file_away('f')
>>> c.files
['f']
"""
self.files.append(file)
def safe_file(self, file, code):
""" (Cabinet, file, str) -> NoneType
Precondition: code must be unique for every file
Store a safe file in self
>>> c = Cabinet()
>>> c.safe_file('f', '059401')
>>> c.files
[('059401', 'f')]
"""
if isinstance(code, str):
self.files.append((code, file))
def __str__(self):
""" (Cabinet) -> str
Return a string version of self
>>> c = Cabinet()
>>> c.file_away(5001)
>>> c.file_away(['1', '6'])
>>> c.safe_file('f', '059401')
>>> print(c)
Cabinet:
[5001, ['1', '6']]
1 safe file(s)
"""
files = []
safe_files = 0
for i in self.files:
if isinstance(i, tuple) and len(i) == 2:
safe_files = safe_files + 1
else:
files.append(i)
return 'Cabinet: \n{0}\n{1} safe file(s)'.format(files, safe_files)
def __repr__(self):
""" (Cabinet) -> str
Return a shell representation of self
>>> c = Cabinet()
>>> c.file_away(5001)
>>> c.file_away(['1', '6'])
>>> c.safe_file('f', '059401')
>>> c
Cabinet([5001, ['1', '6']])
"""
files = []
safe_files = 0
for i in self.files:
if not (isinstance(i, tuple) and len(i) == 2):
files.append(i)
return 'Cabinet({})'.format(files)
def file_search(self, file):
""" (Cabinet, file) -> index/str
Precondition: file must not be a tuple of len 2
Return the index that the file is in, provided it exists in self
>>> c = Cabinet()
>>> c.file_away(1)
>>> c.file_away(2)
>>> c.file_away(3)
>>> c.file_search(2)
1
>>> c.file_search(4)
'That file does not exist'
"""
for i in range(len(self.files)):
if self.files[i] == file:
return i
return 'That file does not exist'
def reveal_safe_file(self, code):
""" (Cabinet, str) -> file/str
Return file by using code as an attempt
>>> c = Cabinet()
>>> c.safe_file('shh...', '1-1-3-1')
>>> c.safe_file(42, 'meaning_of_life')
>>> c.reveal_safe_file('meaning_of_life')
42
>>> c.reveal_safe_file('qwertyasdfzxcv')
'No file with that code exists'
"""
safe_files = []
for i in self.files:
if isinstance(i, tuple) and len(i) == 2:
safe_files.append(i)
for file in safe_files:
if file[0] == code:
return file[1]
return 'No file with that code exists'
if __name__ == '__main__':
import doctest
doctest.testmod()
No comments:
Post a Comment