Skip to content

Commit

Permalink
chore: make ruff happy
Browse files Browse the repository at this point in the history
  • Loading branch information
eri24816 committed Apr 23, 2024
1 parent ad077ad commit 84a1891
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 56 deletions.
5 changes: 3 additions & 2 deletions backend/src/grapycal/sobjects/controls/textControl.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from enum import Enum

from objectsync import IntTopic, StringTopic

from grapycal.sobjects.controls.control import ValuedControl
from grapycal.utils.misc import Action
from grapycal.utils.suggestion import _get_attr_suggestions
from objectsync import StringTopic, IntTopic


class TextControl(ValuedControl[str]):
Expand Down
11 changes: 6 additions & 5 deletions backend/src/grapycal/sobjects/edge.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import random
from typing import Any
from grapycal.sobjects.port import InputPort, OutputPort, Port
from objectsync import SObject, StringTopic, ObjTopic, IntTopic

from objectsync import ObjTopic, SObject, StringTopic
from objectsync.sobject import SObjectSerialized

from grapycal.sobjects.port import InputPort, OutputPort, Port

try:
import torch
HAS_TORCH = True
except:
except ImportError:
HAS_TORCH = False

try :
import numpy as np
HAS_NUMPY = True
except:
except ImportError:
HAS_NUMPY = False

class Edge(SObject):
Expand Down
11 changes: 5 additions & 6 deletions backend/src/grapycal/sobjects/port.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import abc
from hmac import new
from typing import TYPE_CHECKING, Any, List, Literal
import typing
from typing import TYPE_CHECKING, Any, List

from objectsync import IntTopic, SObject, StringTopic

from grapycal.sobjects.controls.control import ValuedControl
from grapycal.sobjects.controls.nullControl import NullControl
from objectsync import SObject, StringTopic, IntTopic

from grapycal.utils.misc import Action

if TYPE_CHECKING:
from grapycal.sobjects.node import Node
from grapycal.sobjects.edge import Edge
from grapycal.sobjects.node import Node


class Port(SObject):
Expand Down
19 changes: 9 additions & 10 deletions backend/src/grapycal/utils/httpResource.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import json
from pathlib import Path
import traceback
from typing import Any, Generic, Type, TypeVar
from urllib.parse import ParseResult, urlparse
import aiohttp
from grapycal.utils.misc import as_type
import yaml
import logging
import certifi
import ssl
from typing import Generic, Type, TypeVar

import aiohttp
import certifi
import yaml
from aiohttp import TCPConnector

from grapycal.utils.misc import as_type

logger = logging.getLogger(__name__)

T = TypeVar("T")


class HttpResource(Generic[T]):
def __init__(self, url: str, data_type: Type[T] = Any, format=None):
def __init__(self, url: str, data_type: Type[T] = type, format=None):
self.url = url
self.data: T | None = None
self.failed = False
Expand All @@ -35,7 +34,7 @@ def __init__(self, url: str, data_type: Type[T] = Any, format=None):
async def is_avaliable(self):
try:
await self.get()
except:
except Exception:
return False
return True

Expand Down
17 changes: 10 additions & 7 deletions backend/src/grapycal/utils/io.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import asyncio
import gzip
import io
import json
import logging
import os
import threading
from functools import partial
from typing import Any, Callable, Tuple

import grapycal

logger = logging.getLogger(__name__)

import json
import asyncio
import io
import threading
from typing import Any, Callable, Tuple

class OutputStream:
def __init__(self, on_flush:Callable[[str],None], hz=20):
Expand Down Expand Up @@ -82,7 +84,8 @@ def close(self):
self._exit_flag = True
self._enable_flush_event.set()

from functools import partial


def write_workspace(path:str,metadata,data:Any,compress=False):
if not compress:
open_func = partial(open,path,'w',encoding='utf-8')
Expand Down Expand Up @@ -129,7 +132,7 @@ def read_workspace(path,metadata_only=False) -> Tuple[str,Any,Any]:

def file_exists(path):
try:
with open(path,'r') as f:
with open(path,'r'):
return True
except FileNotFoundError:
return False
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from contextlib import contextmanager
from typing import Generator, Tuple

import matplotlib
from grapycal import FloatTopic, StringTopic, to_numpy
from grapycal.sobjects.edge import Edge
from grapycal.sobjects.node import Node
Expand All @@ -11,12 +12,6 @@
from matplotlib.axes import Axes
from matplotlib.figure import Figure

plt.style.use("dark_background")
import matplotlib

matplotlib.use("Agg")


try:
import torch

Expand All @@ -36,6 +31,10 @@
except ImportError:
HAS_PIL = False


plt.style.use("dark_background")
matplotlib.use("Agg")

def render_from_fig(
fig: Figure,
x_range: tuple[float, float]|None = None,
Expand Down Expand Up @@ -395,7 +394,7 @@ def preprocess_data(self, data):
unsliced_data = data
try:
data = eval(f"unsliced_data[{slice_string}]", globals(), locals())
except:
except Exception:
self.slice.text.set(":")

if data.ndim == 2:
Expand Down
5 changes: 2 additions & 3 deletions extensions/grapycal_torch/dataloader.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from typing import Iterator
from grapycal import Node, IntTopic
from grapycal.extension.utils import NodeInfo
from grapycal import IntTopic, Node
from grapycal.sobjects.edge import Edge
from grapycal.sobjects.port import InputPort
from grapycal.stores import main_store
from torch.utils.data import DataLoader


class DataLoaderNode(Node):
category = 'torch/dataloader'

Expand Down
11 changes: 4 additions & 7 deletions extensions/grapycal_torch/generative.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from grapycal import Node, StringTopic
from grapycal.extension.utils import NodeInfo
from grapycal.sobjects.sourceNode import SourceNode
import numpy as np
import math

# ported from https://github.com/pvigier/perlin-numpy/blob/master/perlin2d.py

import torch
import math
from grapycal import FunctionNode, StringTopic
from grapycal.extension.utils import NodeInfo
from grapycal.sobjects.sourceNode import SourceNode

from .settings import SettingsNode

Expand Down Expand Up @@ -200,7 +198,6 @@ def task(self):
self.out_port.push(torch.arange(start, stop, step, device=device))


from grapycal import FunctionNode


class Arange2Node(FunctionNode):
Expand Down
2 changes: 1 addition & 1 deletion extensions/grapycal_torch/networkDef.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def build_node(self,name:str="Network",outputs:List[str]|None=None):
self.network_name.add_validator(lambda x,_: x != '') # empty name may confuse users
try:
self.restore_attributes('network name')
except:
except Exception:
self.network_name.set(name)

self.network_name.set(find_next_valid_name(self.network_name.get(),self.ext.net.outs))
Expand Down
6 changes: 4 additions & 2 deletions extensions/grapycal_torch/normalize.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from grapycal import FloatTopic, IntTopic
from grapycal.extension.utils import NodeInfo
from .moduleNode import SimpleModuleNode
from torch import nn
from grapycal import IntTopic, Node, FloatTopic

from .moduleNode import SimpleModuleNode


class BatchNorm2dNode(SimpleModuleNode):
category = 'torch/neural network'
Expand Down
7 changes: 4 additions & 3 deletions extensions/grapycal_torch/tensor.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from typing import Any, Dict
from typing import Any

import torch
from grapycal import FloatTopic, IntTopic, StringTopic, TextControl
from grapycal.extension.utils import NodeInfo
from grapycal.sobjects.functionNode import FunctionNode
from grapycal.sobjects.sourceNode import SourceNode
import torch
from grapycal import Node, TextControl, StringTopic, FloatTopic, IntTopic


class ZeroesNode(SourceNode):
Expand Down
1 change: 0 additions & 1 deletion extensions/grapycal_torch/tensor_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ def build_node(self):
def calculate(self, x:torch.Tensor, kernel:torch.Tensor):
is_c1hw = False
orig_x = x
orig_kernel = kernel
if len(x.shape) == 2:
x = x.unsqueeze(0)
if len(kernel.shape) == 2:
Expand Down
5 changes: 3 additions & 2 deletions extensions/grapycal_torch/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

from typing import TYPE_CHECKING, Iterable

from grapycal import OptionControl

from typing import TYPE_CHECKING, Iterable
if TYPE_CHECKING:
from grapycal_torch import GrapycalTorch

Expand Down Expand Up @@ -51,7 +52,7 @@ def find_next_valid_name(name:str, invalids:Iterable[str]):
number = name[name.rfind('(')+1:-1]
try:
number = int(number)
except:
except Exception:
base = name
number = 1

Expand Down

0 comments on commit 84a1891

Please sign in to comment.