• Stars
    star
    147
  • Rank 251,347 (Top 5 %)
  • Language
    Python
  • License
    MIT License
  • Created almost 8 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🔥 Python file and zip operations made easy

Flametree Logo

Python file operations made easy

GitHub CI build status

Flametree is a Python library which provides a simple syntax for handling files and folders (no os.path.join, os.listdir etc.), and works the same way for different file systems.

Write a Flametree program to read/write files in disk folders, and your code will also be able to read/write in zip archives and virtual (in-memory) archives - which is particularly useful on web servers.

As an illustration, here is how to use Flametree to read a file texts/poems/the_raven.txt, replace all occurences of the word "raven" by "seagull" in the text, and write the result to a new file the_seagull.txt in the same folder:

from flametree import file_tree

with file_tree("texts") as root:
    poem_text = root.poems.the_raven_txt.read()
    new_text = poem_text.replace("raven", "seagull")
    root.poems._file("the_seagull.txt").write(new_text)

Even in this very simple use case, the syntax is clearer than the os way, which would write as follows:

import os

with open(os.path.join("poems", "the_raven.txt"), "r") as f:
    poem_text = f.read()
new_text = poem_text.replace("raven", "seagull")
with open(os.path.join("poems", "the_seagull.txt"), "w") as f:
    content = f.write(new_text)

Moreover, the same Flametree code also works for files inside a zip archive:

with file_tree("my_archive.zip") as root:
    poem_text = root.poems.the_raven_txt.read()
    new_text = poem_text.replace("raven", "seagull")
    root.poems._file("the_seagull.txt").write(new_text)

Now in hard mode: suppose that your server receives binary zip data of an archive containing poems/the_raven.txt, and must return back a new zip containing a file poems/the_seagull.txt. Here again, the syntax of the core operations is the same:

destination_zip = file_tree("@memory") # Create a new virtual zip
with file_tree(the_raven_zip_data) as root:
    poem_text = root.poems.the_raven_txt.read()
    new_text = poem_text.replace("raven", "seagull")
    destination_zip._dir("poems")._file("the_seagull.txt").write(new_text)
destination_zip_data = destination_zip._close()
# Now send the data to the client

See section Usage below for more examples and features.

Installation

Flametree should work on Windows/Max/Linux, with Python 2 and 3, and has no external dependency.

It can be installed by unzipping the source code in one directory and using this command:

python setup.py install

You can also install it directly from the Python Package Index with this command:

pip install flametree

Contribute

Flametree is an open-source software originally written by Zulko and released on Github under the MIT licence (Copyright Edinburgh Genome Foundry). Everyone is welcome to contribute! In particular if you have ideas of new kinds of file systems to add to Flametree.

Usage

Opening a file tree

Here is how you open different kinds of file systems:

from flametree import file_tree

# Open a directory from the disk's file system:
root = file_tree("my_folder/")

# Open a zip archive on the disk:
root = file_tree("my_archive.zip")

# Connect to a file-like object (file handle, StringIO...) of a zip:
root = file_tree(file_like_object)

# Create a virtual 'in-memory' zip file:
root = file_tree("@memory")

# Open some data string representing a zip to read
root = file_tree(some_big_zip_data_string)

In the two first examples, if my_folder or my_archive.zip do not exist, they will be automatically created. If they do exist, it is possible to completely overwrite them with the option replace=True.

Exploring a file tree:

Once you have created the root element with one of the methods above, you can display the whole file tree with root._tree_view() :

>>> print (root._tree_view())
texts/
  poems/
    dover_beach.txt
    the_raven.txt
    the_tyger.txt
  todo_list.txt
figures/
  figure1.png
  figure2.png
Readme.md

The attributes of a directory like root are its files and subdirectories. For instance to print the content of dover_beach.txt you would write:

print( root.texts.poems.dover_beach_txt.read() )

or even simpler:

root.texts.poems.dover_beach_txt.print_content()
Notice that the . before txt was replaced by _ so as to form a valid
attribute name.

This syntactic sugar is particularly useful to explore a file tree in IPython Notebooks or other editors offering auto-completion:

[illustration]

Alternatively, you can access files and directories using dictionary calls:

root["texts"]["poems"]["dover_beach.txt"]

To iterate through the subdirectories of a directory, use the _dirs attribute:

for subdirectory in root._dirs:
    print (subdirectory._name) # Will print 'texts' and 'figures'

To iterate through the files of a directory, use the _files attribute:

for f in root.figures._files:
    print (f._name) # Will print 'figure1.png' and 'figure2.png'

Finally, use _all_files to iterate through all files nested in a directory. The snippet below prints the content of all .txt files in the file tree:

for f in root._all_files:
    if f._name.endswith(".txt"):
        f.print_content()

Creating files and folders

To create a new subdirectory use _dir:

root._dir("data") # create a 'data' folder at the root
root.data._dir("reports") # create a 'reports' folder under `root/data`

To create a new file use _file:

root._file("joke.txt") # create a 'joke.txt' file at the root.
root.texts._file("hello.txt") # create 'hello.txt' in `root/texts`.

To write content in a file, use .write:

root.joke_txt.write("A plateau is the highest form of flattery.")

Writing to a file will use mode a (append) by default. To overwrite the file set the write mode to "w". Let's erase and rewrite that joke.txt:

root.joke_txt.write("'DNA' stands for National Dyslexic Association.", "w")

File and directory creation commands can be chained. Let us create some new folders data/ and data/test_1/, and write to file data/test_1/values.csv, all in a single line:

root._dir("data")._dir("test_1")._file("values.csv").write("1,15,25")

Beware that ._dir and ._file overwrite their target by default, which means that if you write:

root._dir("data")._file("values_1.csv").write("1,4,7")
root._dir("data")._file("values_2.csv").write("2,9,7")

The directory data will only contain values_2.csv, because the second line's _dir("data") erases the data directory and starts a new one. To avoid this, either use root.data in the second line:

root._dir("data")._file("values_1.csv").write("1,4,7")
root.data._file("values_2.csv").write("2,9,7")

Or use replace=False in _dir:

root._dir("data")._file("values_1.csv").write("1,4,7")
root._dir("data", replace=False)._file("values_2.csv").write("2,9,7")

Other operations

You can move, copy, and delete a file with .move(folder), .copy(folder), .delete(), and a directory with ._move(folder), ._copy(folder), ._delete().

root.data.values1_csv.delete() # delete file 'values1.csv'
root.data._delete() # delete directory 'data'
# Move folder `plots` from `root/figures` to `other_root/figures`
root.figures.plots._move(other_root.figures)
# Move file `fig.png` from `root/figures` to `other_root/figures`
root.figures.fig_png.move(other_root.figures)

Special rules for ZIP archives

It is not currently possible to modify/delete a file that is already zipped into an archive (because zips are not really made for that, it would be doable but would certainly be a hack).

When creating files and folders in a zip with Flametree, the changes in the actual zip will only be performed by closing the root with root._close() (after which the root can't be used any more). If it is an in-memory zip, root._close() returns the value of the zip content as a string (Python 2) or bytes (Python 3).

Here are a few examples:

root = file_tree("archive.zip")
root._file("hello.txt").write("Hi there !")
root._close()

# Equivalent to the previous, using `with`:
with file_tree("archive.zip") as root:
    root._file("hello.txt").write("Hi there !")

# Getting binary data of an in-memory zip file:
root = file_tree("@memory")
root._file("hello.txt").write("Hi there !")
binary_data = root._close()

Using file writers from other libraries

Some libraries have file-generating methods which expect a file name or a file object to write too. You can also feed Flametree files to these functions. for instance here is how to use Weasyprint to create a PDF pdfs/report.pdf

import weasyprint
from flametree import file_tree
root = file_tree(".") # or 'archive.zip' to write in an archive.
html = weasyprint.HTML(string="<b>Hello</b> world!", base_url='.')
html.write_pdf(root._dir("pdfs")._file("test.pdf"))

And here is how you would save a Matplotlib figure in a zip archive:

import matplotlib.pyplot as plt
from flametree import file_tree
fig, ax = plt.subplots(1)
ax.plot([1, 2, 3], [3, 1, 2])
with file_tree("archive.zip") as root:
    fig.savefig(root._dir("plots")._file("figure.png"), format="png")

That's all folks !

More Repositories

1

DnaFeaturesViewer

👁️ Python library to plot DNA sequence features (e.g. from Genbank files)
Python
491
star
2

pdf_reports

📕 Python library and CSS theme to generate PDF reports from HTML/Pug
Python
190
star
3

DnaChisel

✏️ A versatile DNA sequence optimizer
Python
176
star
4

Taskpacker

🎒 Simple schedule optimization library for Python
Python
135
star
5

blabel

🏷️ Python label/sticker PDF generation. HTML templates, built-in barcodes, qr codes, and other goodies
Python
135
star
6

Proglog

📝 Logs and progress bars manager for Python
Python
93
star
7

lala

🌎 Analyze and generate reports of web logs (NGINX)
Python
60
star
8

DnaCauldron

⚗️ Simple cloning simulator (Golden Gate etc.) for single and combinatorial assemblies
Python
44
star
9

sequenticon

👾 Generate identicons for DNA sequences with Python
Python
36
star
10

Plateo

🤖 Python biolab automation library: parsers, reports generators, picklists simulators, and more
Python
35
star
11

Primavera

🌸 Python library for primer-based verification of DNA assemblies: primer selection, data analyis, etc.
Python
32
star
12

codon-usage-tables

📊 Codon usage tables in code-friendly format + Python bindings
Python
31
star
13

Geneblocks

💠 Find common blocks and differences between DNA sequences
Python
28
star
14

crazydoc

Read DNA sequences from colourful Microsoft Word documents
Python
24
star
15

DnaWeaver

A route planner for DNA assembly
Python
22
star
16

Caravagene

🎨 Python library to plot multi-part genetic constructs
Python
21
star
17

genome_collector

🌠 Easily download genomes and build BLAST/Bowtie indexes in Python
Python
20
star
18

CAB

🚖 The friendly Computational App Boilerplate. Django + Vue.JS + Redis queues + NginX
Vue
20
star
19

CUBA

🏖️ The EGF Collection of Useful Bio Apps - Web demos of EGF software
Vue
19
star
20

BandWagon

🎺 Plot DNA digestion band patterns with Python
Python
17
star
21

BandWitch

💫 Computer-aided DNA assembly validation and identification from restriction digests.
Python
15
star
22

SBOL-Visual-CSS

➰ Draw genetic elements with HTML & CSS
HTML
15
star
23

genedom

Batch domestication of genetic parts with Python
Python
13
star
24

Sequeduct

Sequencing analysis pipeline
Nextflow
12
star
25

bioprinter

🖨️ Print pictures with living micro-organisms !
Python
12
star
26

Minotaor

An amino acid sequence annotator
Python
11
star
27

tatapov

🐾 DNA overhang misannealing data for Python
Python
9
star
28

GoldenHinges

🔗 Short overhangs design for DNA assembly
Python
9
star
29

zymp

✂️ Design compact restriction sites arrays (python utility)
Python
8
star
30

kappagate

🔮 Predict DNA assembly clone validity rates - powered by Kappa
Python
8
star
31

dab

👉 EGF Design and Build, the Foundry's DNA design ordering portal
Vue
8
star
32

egf-shared-documents

📚 Shared slideshows, courses, etc. from the EGF
HTML
7
star
33

easy_dna

🎒 Python library to read, write, edit DNA sequences
Python
6
star
34

icebreaker

❄️ Python API for the JBEI-ICE sample manager
Python
6
star
35

Examples

Collection of Python modules and Jupyter notebook examples
HTML
5
star
36

topkappy

🌔 Pythonic bindings for the Kappa model simulation language
Python
5
star
37

crecombio

A simple Cre, Flp and other site-specific recombination simulator
Python
4
star
38

HowTo

📒 Short opinionated recipes, mostly for us.
Python
4
star
39

genedeals

Data on different commercial offers for gene-sized DNA
4
star
40

igem-registry-downloader

Download the full iGEM database of parts
Python
3
star
41

saboteurs

💣 Identify elements impairing success accross group experiments.
Python
3
star
42

Ediacara

Python package for interpreting sequencing data of assembled DNA constructs (plasmids)
Python
3
star
43

DnaWeaver-online

A web app for DNA Weaver
Vue
2
star
44

trellab

Thin layer on top of pytrello for automating Trello organization-based tasks
Python
2
star
45

Polymera

Polymera is a Python package for representing ambiguous sequences written with complement alphabets.
Python
2
star
46

OT2Metclo

MSc project
Python
2
star
47

GeneAlloy

Python
2
star
48

Edinburgh-Genome-Foundry.github.io

📚 The EGF Software Website
HTML
1
star
49

tatapov_data

Data for the EGF Tatapov package
1
star
50

egf-codons-website

📚 Sources of the EGF Codons website, written with Vue.js
Vue
1
star
51

Overhang

Compendium of overhangs
Python
1
star
52

Plasmid_assessor

Plasmid assessment for Golden Gate cloning
Python
1
star
53

ImagesAnnotator

Simple web app to annotate images by hand - built with Vue.js
JavaScript
1
star
54

EGF_Docker_Jupyter

Docker Jupyter images with (almost) all EGF packages
Dockerfile
1
star