From 72e175cdeb80f9141f94c46e7f31e8775036db26 Mon Sep 17 00:00:00 2001 From: Mauricio Baeza Date: Thu, 3 Jun 2021 22:09:34 -0500 Subject: [PATCH] Add examples for calc, writer, base --- doc/published.sh | 3 + doc/source/main/examples_base.rst | 89 +++++++++++++++++++++++++++++ doc/source/main/examples_calc.rst | 22 +++++-- doc/source/main/examples_draw.rst | 2 +- doc/source/main/examples_writer.rst | 11 ++-- source/easymacro.py | 36 ++++++++---- 6 files changed, 143 insertions(+), 20 deletions(-) create mode 100755 doc/published.sh diff --git a/doc/published.sh b/doc/published.sh new file mode 100755 index 0000000..6a5f99c --- /dev/null +++ b/doc/published.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +rsync -ravz build/ elmau.net:/opt/www/doc/zaz/ diff --git a/doc/source/main/examples_base.rst b/doc/source/main/examples_base.rst index 350528c..358119d 100644 --- a/doc/source/main/examples_base.rst +++ b/doc/source/main/examples_base.rst @@ -2,10 +2,99 @@ For Base -------- +You need install ``peewee`` + +``pip install -U peewee`` + Create table ^^^^^^^^^^^^ .. code-block:: python + import easymacro as app + from peewee import * + + database_proxy = DatabaseProxy() + + class BaseModel(Model): + class Meta: + database = database_proxy + legacy_table_names = False + + + class Contacts(BaseModel): + id = IdentityField() + name = CharField() + born = app.BaseDateField(null=True) + + def create_table(): + path = '/home/mau/test.odb' + if app.paths.exists(path): + db = app.docs.connect(path) + else: + db = app.docs.new('base', {'path': path}) + + tables = [Contacts] + db.initialize(database_proxy, tables) return + + +Insert data +^^^^^^^^^^^ + +.. code-block:: python + + def base_insert_data(): + path = '/home/mau/test.odb' + db = app.docs.connect(path) + db.initialize(database_proxy) + + rows = ( + dict(name = 'Ingrid Bergman', born=app.dates.date(2001, 1, 1)), + dict(name = 'Sofia Loren', born=app.dates.date(2002, 2, 2)), + dict(name = 'Kim Novak', born=app.dates.date(2003, 3, 3)), + dict(name = 'Jane Fonda', born=app.dates.date(2004, 4, 4)), + dict(name = 'Marion Cotillar', born=app.dates.date(2005, 5, 5)), + ) + for row in rows: + Contactos.insert(**row).execute() + + return + + +Select data +^^^^^^^^^^^ + +.. code-block:: python + + def base_select_data(): + path = '/home/mau/test.odb' + db = app.docs.connect(path) + db.initialize(database_proxy) + + query = Contactos.select() + rows = db.get_query(query).tuples + for row in rows: + print(row) + + rows = db.get_query(query).dicts + for row in rows: + print(row) + + return + +You can get tuples or dict:: + + (1, 'Ingrid Bergman', datetime.date(2001, 1, 1)) + (2, 'Sofia Loren', datetime.date(2002, 2, 2)) + (3, 'Kim Novak', datetime.date(2003, 3, 3)) + (4, 'Jane Fonda', datetime.date(2004, 4, 4)) + (5, 'Marion Cotillar', datetime.date(2005, 5, 5)) + + {'id': 1, 'name': 'Ingrid Bergman', 'born': datetime.date(2001, 1, 1)} + {'id': 2, 'name': 'Sofia Loren', 'born': datetime.date(2002, 2, 2)} + {'id': 3, 'name': 'Kim Novak', 'born': datetime.date(2003, 3, 3)} + {'id': 4, 'name': 'Jane Fonda', 'born': datetime.date(2004, 4, 4)} + {'id': 5, 'name': 'Marion Cotillar', 'born': datetime.date(2005, 5, 5)} + diff --git a/doc/source/main/examples_calc.rst b/doc/source/main/examples_calc.rst index 1547566..93f45ff 100644 --- a/doc/source/main/examples_calc.rst +++ b/doc/source/main/examples_calc.rst @@ -1,11 +1,23 @@ -Examples for Calc ------------------ +For Calc +-------- -First -^^^^^^^^^^ +Data to cell +^^^^^^^^^^^^ + +Automatic calculate size range. .. code-block:: python - def _(): + def calc_data_to_cell(): + sheet = app.active_sheet + data = ( + ('Month', 'Total'), + ('January', 100), + ('February', 200), + ('March', 300), + ('April', 400), + ('May', 500), + ) + sheet['A1'].data = data return diff --git a/doc/source/main/examples_draw.rst b/doc/source/main/examples_draw.rst index af2bc50..abef9da 100644 --- a/doc/source/main/examples_draw.rst +++ b/doc/source/main/examples_draw.rst @@ -1,5 +1,5 @@ -Examples for Draw +For Draw ----------------- Save image from clipboard diff --git a/doc/source/main/examples_writer.rst b/doc/source/main/examples_writer.rst index b46b71d..d7f3067 100644 --- a/doc/source/main/examples_writer.rst +++ b/doc/source/main/examples_writer.rst @@ -1,11 +1,14 @@ -Examples for Writer +For Writer ------------------- -First -^^^^^ +Set autostyle in table +^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: python - def _(): + def writer_table_set_style(): + doc = app.active + table = doc.tables[0] + table.style = 'Academic' return diff --git a/source/easymacro.py b/source/easymacro.py index c08eaf9..2c7d5f3 100644 --- a/source/easymacro.py +++ b/source/easymacro.py @@ -3665,15 +3665,19 @@ class BaseRow: class BaseQuery(object): PY_TYPES = { - 'SQL_LONG': 'getLong', - 'SQL_VARYING': 'getString', - 'SQL_FLOAT': 'getFloat', - 'SQL_BOOLEAN': 'getBoolean', - 'SQL_TYPE_DATE': 'getDate', - 'SQL_TYPE_TIME': 'getTime', - 'SQL_TIMESTAMP': 'getTimestamp', + 'VARCHAR': 'getString', + 'INTEGER': 'getLong', + 'DATE': 'getDate', + # ~ 'SQL_LONG': 'getLong', + # ~ 'SQL_VARYING': 'getString', + # ~ 'SQL_FLOAT': 'getFloat', + # ~ 'SQL_BOOLEAN': 'getBoolean', + # ~ 'SQL_TYPE_DATE': 'getDate', + # ~ 'SQL_TYPE_TIME': 'getTime', + # ~ 'SQL_TIMESTAMP': 'getTimestamp', } - TYPES_DATE = ('SQL_TYPE_DATE', 'SQL_TYPE_TIME', 'SQL_TIMESTAMP') + # ~ TYPES_DATE = ('SQL_TYPE_DATE', 'SQL_TYPE_TIME', 'SQL_TIMESTAMP') + TYPES_DATE = ('DATE', 'SQL_TYPE_TIME', 'SQL_TIMESTAMP') def __init__(self, query): self._query = query @@ -3699,6 +3703,7 @@ class BaseQuery(object): def _to_python(self, index): type_field = self._meta.getColumnTypeName(index) + # ~ print('TF', type_field) value = getattr(self._query, self.PY_TYPES[type_field])(index) if type_field in self.TYPES_DATE: value = _struct_to_date(value) @@ -3824,10 +3829,11 @@ class LOBase(object): self._con.getTables().refresh() return - def initialize(self, database_proxy, tables): + def initialize(self, database_proxy, tables=[]): db = FirebirdDatabase(self) database_proxy.initialize(db) - db.create_tables(tables) + if tables: + db.create_tables(tables) return def _validate_sql(self, sql, params): @@ -6482,6 +6488,14 @@ class Paths(object): _P = Paths +class Dates(object): + + @classmethod + def date(cls, year, month, day): + d = datetime.date(year, month, day) + return d + + class SpellChecker(object): def __init__(self): @@ -6543,6 +6557,8 @@ def __getattr__(name): return LOShortCuts() if name == 'clipboard': return ClipBoard + if name == 'dates': + return Dates raise AttributeError(f"module '{__name__}' has no attribute '{name}'")