Skip to content

Commit

Permalink
Better meta class
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Mancevice committed Aug 16, 2016
1 parent 1e8060c commit 01b7411
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,22 @@
Query InfluxDB using SQLAlchemy-style syntax


## Installation

```bash
pip install influxalchemy
```


## Usage

```python
import influxdb
import influxalchemy
```

## Define InfluxAlchemy Measurements

### Define InfluxAlchemy Measurements

```python
class Widgets(influxalchemy.Measurement):
Expand All @@ -25,7 +34,10 @@ class Wombats(influxalchemy.Measurement):
__measurement__ = 'wombats'
```

## Open InfluxAlchemy Connection
The class-attribute `__measurement__` can be omitted and will default to the class name if absent.


### Open InfluxAlchemy Connection


```python
Expand Down Expand Up @@ -99,7 +111,7 @@ flux.query(Widgets).filter(clause1 | clause2)
```


### Group Bys
### Group By

```python
# SELECT * FROM widgets GROUP BY time(1d);
Expand All @@ -119,3 +131,5 @@ flux.query(Widgets).filter(Widgets.time > "now() - 7d")
# SELECT * FROM widgets WHERE time >= '2016-01-01' AND time <= now() - 7d;
flux.query(Widgets).filter(Widgets.time.between("'2016-01-01'", "now() - 7d"))
```

Note that the date is wrapped in single-quotes (`'`).
2 changes: 1 addition & 1 deletion influxalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
from .client import InfluxAlchemy
from .measurement import Measurement

__version__ = "0.0.2"
__version__ = "0.0.3"
17 changes: 12 additions & 5 deletions influxalchemy/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@

class MetaMeasurement(type):
""" Meta class of Measurement. """
def __getattr__(cls, name):
if name == "time":
return Time(name, cls)
else:
return Tag(name, cls)
def __new__(mcs, name, bases, dict_):
dict_.setdefault("__measurement__", name)
return super(MetaMeasurement, mcs).__new__(mcs, name, bases, dict_)

def __getattribute__(cls, name):
try:
return super(MetaMeasurement, cls).__getattribute__(name)
except AttributeError:
if name == "time":
return Time(name, cls)
else:
return Tag(name, cls)

def __str__(cls):
return cls.__measurement__
Expand Down

0 comments on commit 01b7411

Please sign in to comment.