FileDataSource
laktory.models.FileDataSource
¤
Bases: BaseDataSource
Data source using disk files, such data events (json/csv) or full dataframes.
Examples:
from laktory import models
source = models.FileDataSource(
path="/Volumes/sources/landing/events/yahoo-finance/stock_price",
format="JSON",
dataframe_backend="POLARS",
)
# df = source.read()
# With Explicit Schema
source = models.FileDataSource(
path="/Volumes/sources/landing/events/yahoo-finance/stock_price",
format="JSON",
dataframe_backend="PYSPARK",
schema={
"columns": {
"symbol": "String",
"open": "Float64",
"close": "Float64",
}
},
)
# df = source.read()
PARAMETER | DESCRIPTION |
---|---|
dataframe_backend_
|
Type of DataFrame backend
TYPE:
|
dataframe_api_
|
DataFrame API to use in DataFrame Transformer nodes. Either 'NATIVE' (backend-specific) or 'NARWHALS' (backend-agnostic).
TYPE:
|
variables
|
Dict of variables to be injected in the model at runtime |
as_stream
|
If
TYPE:
|
drop_duplicates
|
Remove duplicated rows from source using all columns if |
drops
|
List of columns to drop
TYPE:
|
filter
|
SQL expression used to select specific rows from the source table
TYPE:
|
renames
|
Mapping between the source column names and desired column names
TYPE:
|
selects
|
Columns to select from the source. Can be specified as a list or as a dictionary to rename the source columns
TYPE:
|
type
|
Source Type
TYPE:
|
format
|
Format of the data files.
TYPE:
|
has_header
|
Indicate if the first row of the dataset is a header or not. Only applicable to 'CSV' format.
TYPE:
|
infer_schema
|
When
TYPE:
|
path
|
File path on a local disk, remote storage or Databricks volume.
TYPE:
|
reader_kwargs
|
Keyword arguments passed directly to dataframe backend reader. Passed to
TYPE:
|
schema_definition
|
Target schema specified as a list of columns, as a dict or a json serialization. Only used when reading data from non-strongly typed files such as JSON or csv files.
TYPE:
|
schema_location_
|
Path for schema inference when reading data as a stream. If |
reader_methods
|
DataFrame backend reader methods.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
inject_vars |
Inject model variables values into a model attributes. |
inject_vars_into_dump |
Inject model variables values into a model dump. |
model_validate_json_file |
Load model from json file object |
model_validate_yaml |
Load model from yaml file object using laktory.yaml.RecursiveLoader. Supports |
push_vars |
Push variable values to all child recursively |
read |
Read data with options specified in attributes. |
validate_assignment_disabled |
Updating a model attribute inside a model validator when |
inject_vars(inplace=False, vars=None)
¤
Inject model variables values into a model attributes.
PARAMETER | DESCRIPTION |
---|---|
inplace
|
If
TYPE:
|
vars
|
A dictionary of variables to be injected in addition to the model internal variables.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Model instance. |
Examples:
from typing import Union
from laktory import models
class Cluster(models.BaseModel):
name: str = None
size: Union[int, str] = None
c = Cluster(
name="cluster-${vars.my_cluster}",
size="${{ 4 if vars.env == 'prod' else 2 }}",
variables={
"env": "dev",
},
).inject_vars()
print(c)
# > variables={'env': 'dev'} name='cluster-${vars.my_cluster}' size=2
References
Source code in laktory/models/basemodel.py
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 |
|
inject_vars_into_dump(dump, inplace=False, vars=None)
¤
Inject model variables values into a model dump.
PARAMETER | DESCRIPTION |
---|---|
dump
|
Model dump (or any other general purpose mutable object) |
inplace
|
If
TYPE:
|
vars
|
A dictionary of variables to be injected in addition to the model internal variables. |
RETURNS | DESCRIPTION |
---|---|
Model dump with injected variables. |
Examples:
from laktory import models
m = models.BaseModel(
variables={
"env": "dev",
},
)
data = {
"name": "cluster-${vars.my_cluster}",
"size": "${{ 4 if vars.env == 'prod' else 2 }}",
}
print(m.inject_vars_into_dump(data))
# > {'name': 'cluster-${vars.my_cluster}', 'size': 2}
References
Source code in laktory/models/basemodel.py
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 |
|
model_validate_json_file(fp)
classmethod
¤
Load model from json file object
PARAMETER | DESCRIPTION |
---|---|
fp
|
file object structured as a json file
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Model
|
Model instance |
Source code in laktory/models/basemodel.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
|
model_validate_yaml(fp)
classmethod
¤
Load model from yaml file object using laktory.yaml.RecursiveLoader. Supports
reference to external yaml and sql files using !use
, !extend
and !update
tags.
Path to external files can be defined using model or environment variables.
Referenced path should always be relative to the file they are referenced from.
PARAMETER | DESCRIPTION |
---|---|
fp
|
file object structured as a yaml file
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Model
|
Model instance |
Examples:
businesses:
apple:
symbol: aapl
address: !use addresses.yaml
<<: !update common.yaml
emails:
- jane.doe@apple.com
- extend! emails.yaml
amazon:
symbol: amzn
address: !use addresses.yaml
<<: update! common.yaml
emails:
- john.doe@amazon.com
- extend! emails.yaml
Source code in laktory/models/basemodel.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
|
push_vars(update_core_resources=False)
¤
Push variable values to all child recursively
Source code in laktory/models/basemodel.py
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
|
read(**kwargs)
¤
Read data with options specified in attributes.
RETURNS | DESCRIPTION |
---|---|
AnyFrame
|
Resulting dataframe |
Source code in laktory/models/datasources/basedatasource.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
|
validate_assignment_disabled()
¤
Updating a model attribute inside a model validator when validate_assignment
is True
causes an infinite recursion by design and must be turned off
temporarily.
Source code in laktory/models/basemodel.py
341 342 343 344 345 346 347 348 349 350 351 352 353 |
|