Skip to content

Instantly share code, notes, and snippets.

View pandorica-opens's full-sized avatar

Hanna Kondratiuk pandorica-opens

View GitHub Profile
@pandorica-opens
pandorica-opens / magic_memit.py
Created February 15, 2021 00:54 — forked from vene/magic_memit.py
memit: magic memory usage benching for IPython
# Author: Vlad Niculae <vlad@vene.ro>
# Makes use of memory_profiler from Fabian Pedregosa
# available at https://github.com/fabianp/memory_profiler
from IPython.core.magic import Magics, line_magic, magics_class
class MemMagics(Magics):
@line_magic
def memit(self, line='', setup='pass'):
#!/usr/bin/env python3
import sys
import subprocess
print(sys.argv[1])
with open(sys.argv[1]) as file:
changed = file.read().replace('jane', 'jdoe')
file.seek(0)
@pandorica-opens
pandorica-opens / findJane.sh
Created January 18, 2021 20:33
piping with bash
#!/bin/bash
> oldFiles.txt
files=$(grep ' jane ' ../data/list.txt | cut -d ' ' -f 3 -s) #--output-delimiter='/n')
echo $files
cd ..
echo $PWD
@pandorica-opens
pandorica-opens / flowers_csvreader.py
Created October 26, 2020 12:53
Process the flowers data again without turning it into a dictionary.
import os
import csv
# Create a file with data in it
def create_file(filename):
with open(filename, "w") as file:
file.write("name,color,type\n")
file.write("carnation,pink,annual\n")
file.write("daffodil,yellow,perennial\n")
file.write("iris,blue,perennial\n")
@pandorica-opens
pandorica-opens / flowers_list_dictreader.py
Created October 26, 2020 12:51
We're working with a list of flowers and some information about each one. The create_file function writes this information to a CSV file. The contents_of_file function reads this file into records and returns the information in a nicely formatted block. Turns the data in the CSV file into a dictionary using DictReader.
import os
import csv
# Create a file with data in it
def create_file(filename):
with open(filename, "w") as file:
file.write("name,color,type\n")
file.write("carnation,pink,annual\n")
file.write("daffodil,yellow,perennial\n")
file.write("iris,blue,perennial\n")
@pandorica-opens
pandorica-opens / gist_to_github_repo.md
Created October 26, 2020 12:23 — forked from ishu3101/gist_to_github_repo.md
Transfer a gist to a GitHub repository

Transfer a gist to a GitHub repository

clone the gist

git clone https://gist.github.com/ishu3101/6fb35afd237e42ef25f9

rename the directory

mv 6fb35afd237e42ef25f9 ConvertTo-Markdown

change the working directory to the newly renamed directory

cd ConvertTo-Markdown

@pandorica-opens
pandorica-opens / new_directory.py
Created October 26, 2020 11:58
The new_directory function creates a new directory inside the current working directory, then creates a new empty file inside the new directory, and returns the list of files in that directory.
import os
def new_directory(directory, filename):
# Before creating a new directory, check to see if it already exists
if os.path.isdir(directory) == False:
os.mkdir(directory)
# Create the new file inside of the new directory
os.chdir(directory)
if os.path.isfile(filename) == False:
@pandorica-opens
pandorica-opens / parent_directory.py
Created October 26, 2020 11:57
The parent_directory function returns the name of the directory that's located just above the current working directory.
import os
def parent_directory():
# Create a relative path to the parent
# of the current working directory
os.chdir('..')
relative_parent = os.path.abspath(os.getcwd())
# Return the absolute path of the parent directory
return relative_parent
@pandorica-opens
pandorica-opens / file_timestamp.py
Created October 26, 2020 11:48
The file_date function creates a new file in the current working directory, checks the date that the file was modified, and returns just the date portion of the timestamp in the format of yyyy-mm-dd.
def file_date(filename):
# Create the file in the current directory
# For mknod the root priveleges are required
os.mknod(filename)
timestamp = os.path.getmtime(filename)
# Convert the timestamp into a readable format, then into a string
time = datetime.datetime.fromtimestamp(timestamp)
time = str(time)
# Return just the date portion
# Hint: how many characters are in “yyyy-mm-dd”?
@pandorica-opens
pandorica-opens / create_script.py
Created October 26, 2020 11:45
The create_python_script function creates a new python script in the current working directory, adds the line of comments to it declared by the 'comments' variable, and returns the size of the new file.
import os
def create_python_script(filename):
comments = "# Start of a new Python program"
with open(filename, 'a+') as f:
f.write(comments)
print(f.read())
filesize = os.path.getsize(filename)
return(filesize)